-
-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathRepairEmailTemplateCtaCommand.php
More file actions
82 lines (60 loc) · 2.29 KB
/
RepairEmailTemplateCtaCommand.php
File metadata and controls
82 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace HiEvents\Console\Commands;
use HiEvents\DomainObjects\Enums\EmailTemplateType;
use HiEvents\Models\EmailTemplate;
use HiEvents\Services\Domain\Email\EmailTemplateService;
use Illuminate\Console\Command;
class RepairEmailTemplateCtaCommand extends Command
{
protected $signature = 'email-templates:repair-cta-url-token
{--dry-run : Show what would change without writing}';
protected $description = 'Reset cta.url_token on email_templates rows to the default for each template_type. Repairs damage from a prior bug where attendee_ticket templates were saved with order.url.';
public function __construct(private readonly EmailTemplateService $emailTemplateService)
{
parent::__construct();
}
public function handle(): int
{
$dryRun = (bool) $this->option('dry-run');
$defaults = [];
foreach (EmailTemplateType::cases() as $type) {
$defaults[$type->value] = $this->emailTemplateService->getDefaultTemplate($type)['cta']['url_token'] ?? null;
}
$rows = EmailTemplate::query()->get();
$changed = 0;
$skipped = 0;
foreach ($rows as $row) {
$type = $row->template_type;
$expected = $defaults[$type] ?? null;
if ($expected === null) {
$skipped++;
continue;
}
$cta = is_array($row->cta) ? $row->cta : (json_decode((string) $row->cta, true) ?: null);
if ($cta === null) {
$skipped++;
continue;
}
$current = $cta['url_token'] ?? null;
if ($current === $expected) {
$skipped++;
continue;
}
$this->line(sprintf(
'id=%d type=%s url_token: %s -> %s',
$row->id,
$type,
var_export($current, true),
var_export($expected, true),
));
if (! $dryRun) {
$cta['url_token'] = $expected;
$row->cta = $cta;
$row->save();
}
$changed++;
}
$this->info(sprintf('%s%d changed, %d unchanged.', $dryRun ? '[dry-run] ' : '', $changed, $skipped));
return self::SUCCESS;
}
}