Files
alrahma_sunday_school/app/Commands/SendRegistrationOpeningEmail.php
T
root 1ef2800f12
Tests / PHPUnit (push) Successful in 1m26s
fix enrollment for the new school-year
2026-08-07 23:43:31 -04:00

55 lines
2.0 KiB
PHP

<?php
namespace App\Commands;
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 = service('enrollmentRegistrationEmail');
$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');
}
}