56 lines
2.1 KiB
PHP
56 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use App\Services\RegistrationOpeningEmailService;
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
use CodeIgniter\CLI\CLI;
|
|
|
|
class SendRegistrationOpeningEmail extends BaseCommand
|
|
{
|
|
protected $group = 'Registration';
|
|
protected $name = 'registration:send-opening-email';
|
|
protected $description = 'Send the parent registration opening email on the configured registration start date.';
|
|
protected $usage = 'php spark registration:send-opening-email [--force] [--date=YYYY-MM-DD] [--email=parent@example.com] [--dry-run] [--tz=America/New_York]';
|
|
|
|
public function run(array $params)
|
|
{
|
|
$tzName = (string) (CLI::getOption('tz') ?? config('School')->attendance['timezone'] ?? 'America/New_York');
|
|
$timezone = new \DateTimeZone($tzName);
|
|
$dateOption = CLI::getOption('date');
|
|
$date = $dateOption
|
|
? new \DateTimeImmutable((string) $dateOption, $timezone)
|
|
: new \DateTimeImmutable('today', $timezone);
|
|
|
|
$force = CLI::getOption('force') !== null;
|
|
$dryRun = CLI::getOption('dry-run') !== null;
|
|
$email = CLI::getOption('email');
|
|
$email = is_string($email) && trim($email) !== '' ? trim($email) : null;
|
|
|
|
if ($email !== null && ! filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
CLI::error('Invalid --email value.');
|
|
return;
|
|
}
|
|
|
|
$service = new RegistrationOpeningEmailService();
|
|
$summary = $service->sendForDate($date, $force, $email, $dryRun);
|
|
|
|
foreach ($summary['messages'] as $message) {
|
|
CLI::write($message, 'yellow');
|
|
}
|
|
|
|
$sentLabel = $dryRun ? 'Would send' : 'Sent';
|
|
|
|
CLI::write(sprintf(
|
|
'Registration opening email complete. Years: %d. Recipients: %d. %s: %d. Failed: %d. Skipped: %d%s.',
|
|
$summary['school_years'],
|
|
$summary['recipients'],
|
|
$sentLabel,
|
|
$summary['sent'],
|
|
$summary['failed'],
|
|
$summary['skipped'],
|
|
$dryRun ? ' (dry run)' : ''
|
|
), $summary['failed'] > 0 ? 'red' : 'green');
|
|
}
|
|
}
|