|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\CertificateExcellence; |
| 6 | +use App\Excellence; |
| 7 | +use Illuminate\Console\Command; |
| 8 | + |
| 9 | +class CertificatePreflight extends Command |
| 10 | +{ |
| 11 | + protected $signature = 'certificate:preflight |
| 12 | + {--edition=2025 : Target edition year} |
| 13 | + {--type=all : excellence|super-organiser|all} |
| 14 | + {--limit=0 : Max records to test (0 = all)} |
| 15 | + {--only-pending : Test only rows without certificate_url} |
| 16 | + {--export= : Optional CSV path for failures}'; |
| 17 | + |
| 18 | + protected $description = 'Dry-run compile certificates (no S3 upload, no DB updates) and report failures'; |
| 19 | + |
| 20 | + public function handle(): int |
| 21 | + { |
| 22 | + $edition = (int) $this->option('edition'); |
| 23 | + $typeOption = strtolower(trim((string) $this->option('type'))); |
| 24 | + $limit = max(0, (int) $this->option('limit')); |
| 25 | + $onlyPending = (bool) $this->option('only-pending'); |
| 26 | + $exportPath = trim((string) $this->option('export')); |
| 27 | + |
| 28 | + $types = $this->resolveTypes($typeOption); |
| 29 | + if ($types === null) { |
| 30 | + $this->error("Invalid --type value: {$typeOption}. Use 'excellence', 'super-organiser', or 'all'."); |
| 31 | + return self::FAILURE; |
| 32 | + } |
| 33 | + |
| 34 | + $query = Excellence::query() |
| 35 | + ->where('edition', $edition) |
| 36 | + ->whereIn('type', $types) |
| 37 | + ->with('user') |
| 38 | + ->orderBy('type') |
| 39 | + ->orderBy('id'); |
| 40 | + |
| 41 | + if ($onlyPending) { |
| 42 | + $query->whereNull('certificate_url'); |
| 43 | + } |
| 44 | + |
| 45 | + if ($limit > 0) { |
| 46 | + $query->limit($limit); |
| 47 | + } |
| 48 | + |
| 49 | + $rows = $query->get(); |
| 50 | + if ($rows->isEmpty()) { |
| 51 | + $this->info('No recipients found for the selected filters.'); |
| 52 | + return self::SUCCESS; |
| 53 | + } |
| 54 | + |
| 55 | + $failures = []; |
| 56 | + $ok = 0; |
| 57 | + $bar = $this->output->createProgressBar($rows->count()); |
| 58 | + $bar->start(); |
| 59 | + |
| 60 | + foreach ($rows as $e) { |
| 61 | + $bar->advance(); |
| 62 | + $user = $e->user; |
| 63 | + if (! $user) { |
| 64 | + $failures[] = $this->failureRow($e, '', 'Missing related user record.'); |
| 65 | + continue; |
| 66 | + } |
| 67 | + if (! $user->email) { |
| 68 | + $failures[] = $this->failureRow($e, (string) $user->email, 'Missing user email.'); |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + $name = $e->name_for_certificate ?? trim(($user->firstname ?? '') . ' ' . ($user->lastname ?? '')); |
| 73 | + if ($name === '') { |
| 74 | + $failures[] = $this->failureRow($e, (string) $user->email, 'Empty certificate holder name.'); |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + $certType = $e->type === 'SuperOrganiser' ? 'super-organiser' : 'excellence'; |
| 79 | + $numberOfActivities = $e->type === 'SuperOrganiser' ? (int) $user->activities($edition) : 0; |
| 80 | + |
| 81 | + try { |
| 82 | + $cert = new CertificateExcellence( |
| 83 | + $edition, |
| 84 | + $name, |
| 85 | + $certType, |
| 86 | + $numberOfActivities, |
| 87 | + (int) $user->id, |
| 88 | + (string) $user->email |
| 89 | + ); |
| 90 | + $cert->preflight(); |
| 91 | + $ok++; |
| 92 | + } catch (\Throwable $ex) { |
| 93 | + $failures[] = $this->failureRow($e, (string) $user->email, $ex->getMessage()); |
| 94 | + } |
| 95 | + } |
| 96 | + $bar->finish(); |
| 97 | + $this->newLine(2); |
| 98 | + |
| 99 | + $this->info("Preflight complete. Tested: {$rows->count()}, Passed: {$ok}, Failed: " . count($failures)); |
| 100 | + |
| 101 | + if (! empty($failures)) { |
| 102 | + $show = array_slice($failures, 0, 20); |
| 103 | + $this->table(['id', 'type', 'user_id', 'email', 'name', 'error'], $show); |
| 104 | + if (count($failures) > 20) { |
| 105 | + $this->line('Showing first 20 failures. Use --export for full list.'); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if ($exportPath !== '') { |
| 110 | + $path = $this->resolvePath($exportPath); |
| 111 | + $dir = dirname($path); |
| 112 | + if (! is_dir($dir) && ! @mkdir($dir, 0775, true) && ! is_dir($dir)) { |
| 113 | + $this->error("Failed to create export directory: {$dir}"); |
| 114 | + return self::FAILURE; |
| 115 | + } |
| 116 | + $fh = @fopen($path, 'wb'); |
| 117 | + if (! $fh) { |
| 118 | + $this->error("Failed to open export file: {$path}"); |
| 119 | + return self::FAILURE; |
| 120 | + } |
| 121 | + fputcsv($fh, ['id', 'type', 'edition', 'user_id', 'email', 'name_for_certificate', 'error']); |
| 122 | + foreach ($failures as $row) { |
| 123 | + fputcsv($fh, [ |
| 124 | + $row['id'], |
| 125 | + $row['type'], |
| 126 | + $edition, |
| 127 | + $row['user_id'], |
| 128 | + $row['email'], |
| 129 | + $row['name'], |
| 130 | + $row['error'], |
| 131 | + ]); |
| 132 | + } |
| 133 | + fclose($fh); |
| 134 | + $this->info("Exported failures CSV: {$path}"); |
| 135 | + } |
| 136 | + |
| 137 | + return self::SUCCESS; |
| 138 | + } |
| 139 | + |
| 140 | + private function resolveTypes(string $typeOption): ?array |
| 141 | + { |
| 142 | + return match ($typeOption) { |
| 143 | + 'all' => ['Excellence', 'SuperOrganiser'], |
| 144 | + 'excellence' => ['Excellence'], |
| 145 | + 'super-organiser', 'superorganiser' => ['SuperOrganiser'], |
| 146 | + default => null, |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + private function resolvePath(string $path): string |
| 151 | + { |
| 152 | + if (str_starts_with($path, '/')) { |
| 153 | + return $path; |
| 154 | + } |
| 155 | + return base_path($path); |
| 156 | + } |
| 157 | + |
| 158 | + private function failureRow(Excellence $e, string $email, string $error): array |
| 159 | + { |
| 160 | + $name = $e->name_for_certificate ?? ($e->user ? trim(($e->user->firstname ?? '') . ' ' . ($e->user->lastname ?? '')) : ''); |
| 161 | + return [ |
| 162 | + 'id' => $e->id, |
| 163 | + 'type' => $e->type, |
| 164 | + 'user_id' => (int) $e->user_id, |
| 165 | + 'email' => $email, |
| 166 | + 'name' => (string) $name, |
| 167 | + 'error' => $error, |
| 168 | + ]; |
| 169 | + } |
| 170 | +} |
0 commit comments