|
4 | 4 |
|
5 | 5 | use App\CertificateExcellence; |
6 | 6 | use Illuminate\Console\Command; |
7 | | -use Illuminate\Support\Facades\Storage; |
8 | 7 |
|
9 | 8 | class CertificateTestTwo extends Command |
10 | 9 | { |
11 | 10 | protected $signature = 'certificate:test-two |
12 | 11 | {--edition=2025 : Edition year} |
13 | | - {--no-pdf : Only run preflight (no S3), do not keep PDFs}'; |
| 12 | + {--type=all : excellence|super-organiser|all} |
| 13 | + {--no-pdf : Preflight only (no S3, no PDFs kept); default} |
| 14 | + {--keep-pdf : Generate PDFs and save locally so you can view them (storage/app/test-certificates/)} |
| 15 | + {--generate : Generate and upload to S3 (use with care)}'; |
14 | 16 |
|
15 | | - protected $description = 'Generate two test certificates locally: one Greek name, one Russian name'; |
| 17 | + protected $description = 'Run preflight for a few test names (Greek, Cyrillic, Latin, accented); use --keep-pdf to save viewable PDFs'; |
| 18 | + |
| 19 | + /** @var array<int, array{name: string, label: string}> */ |
| 20 | + private static array $testNames = [ |
| 21 | + ['name' => 'Βασιλική Μπαμπαλή', 'label' => 'Greek'], |
| 22 | + ['name' => 'Юлія Колісниченко', 'label' => 'Ukrainian (Cyrillic)'], |
| 23 | + ['name' => 'Иван Петров', 'label' => 'Russian (Cyrillic)'], |
| 24 | + ['name' => 'John Smith', 'label' => 'English'], |
| 25 | + ['name' => 'José García', 'label' => 'Accented Latin'], |
| 26 | + ['name' => 'María Fernández', 'label' => 'Accented Latin 2'], |
| 27 | + ]; |
16 | 28 |
|
17 | 29 | public function handle(): int |
18 | 30 | { |
19 | 31 | $edition = (int) $this->option('edition'); |
20 | | - $preflightOnly = (bool) $this->option('no-pdf'); |
| 32 | + $typeOption = strtolower(trim((string) $this->option('type'))); |
| 33 | + $keepPdf = (bool) $this->option('keep-pdf'); |
| 34 | + $uploadToS3 = (bool) $this->option('generate'); |
| 35 | + $preflightOnly = ! $keepPdf && ! $uploadToS3; |
| 36 | + |
| 37 | + $typeList = $this->resolveTypes($typeOption); |
| 38 | + if ($typeList === null) { |
| 39 | + $this->error("Invalid --type: {$typeOption}. Use excellence, super-organiser, or all."); |
| 40 | + return self::FAILURE; |
| 41 | + } |
21 | 42 |
|
22 | | - $tests = [ |
23 | | - ['name' => 'Βασιλική Μπαμπαλή', 'label' => 'Greek'], |
24 | | - ['name' => 'Иван Петров', 'label' => 'Russian'], |
25 | | - ]; |
| 43 | + $outDir = $keepPdf ? storage_path('app/test-certificates') : null; |
| 44 | + if ($outDir !== null && ! is_dir($outDir)) { |
| 45 | + mkdir($outDir, 0775, true); |
| 46 | + } |
26 | 47 |
|
27 | | - foreach ($tests as $test) { |
28 | | - $this->info("Testing {$test['label']}: {$test['name']}"); |
| 48 | + $mode = $preflightOnly ? 'Preflight only (no PDFs kept)' : ($keepPdf ? 'Generating PDFs to ' . $outDir : 'Generating and uploading to S3'); |
| 49 | + $this->info('Certificate test names. Edition: ' . $edition . ', types: ' . implode(', ', $typeList) . '. ' . $mode); |
| 50 | + $this->newLine(); |
| 51 | + |
| 52 | + $failed = 0; |
| 53 | + $index = 0; |
| 54 | + foreach (self::$testNames as $test) { |
| 55 | + $this->line("--- {$test['label']}: {$test['name']} ---"); |
29 | 56 | $cert = new CertificateExcellence( $edition, $test[ 'name'], 'excellence', 0, 999999, '[email protected]'); |
30 | | - $this->line(' is_greek(): ' . ($cert->is_greek() ? 'true' : 'false')); |
31 | | - |
32 | | - try { |
33 | | - if ($preflightOnly) { |
34 | | - $cert->preflight(); |
35 | | - $this->info(" Preflight OK (no PDF kept)."); |
36 | | - } else { |
37 | | - $url = $cert->generate(); |
38 | | - $this->info(" Generated: {$url}"); |
| 57 | + $this->line(' is_greek(): ' . ($cert->is_greek() ? 'true' : 'false') . ', is_cyrillic(): ' . ($cert->is_cyrillic() ? 'true' : 'false')); |
| 58 | + |
| 59 | + $safeLabel = preg_replace('/[^a-zA-Z0-9_-]/', '_', $test['label']); |
| 60 | + foreach ($typeList as $certType) { |
| 61 | + $activities = $certType === 'super-organiser' ? 10 : 0; |
| 62 | + $certTest = new CertificateExcellence( $edition, $test[ 'name'], $certType, $activities, 999999, '[email protected]'); |
| 63 | + $label = $certType === 'super-organiser' ? 'SuperOrganiser' : 'Excellence'; |
| 64 | + try { |
| 65 | + if ($preflightOnly) { |
| 66 | + $certTest->preflight(); |
| 67 | + $this->info(" [{$label}] Preflight OK."); |
| 68 | + } elseif ($keepPdf) { |
| 69 | + $index++; |
| 70 | + $filename = sprintf('%02d-%s-%s.pdf', $index, $safeLabel, $label); |
| 71 | + $path = $certTest->generateAndSavePdfTo($outDir . DIRECTORY_SEPARATOR . $filename); |
| 72 | + $this->info(" [{$label}] Saved: {$path}"); |
| 73 | + } else { |
| 74 | + $url = $certTest->generate(); |
| 75 | + $this->info(" [{$label}] Generated: {$url}"); |
| 76 | + } |
| 77 | + } catch (\Throwable $e) { |
| 78 | + $this->error(" [{$label}] Failed: " . $e->getMessage()); |
| 79 | + $failed++; |
39 | 80 | } |
40 | | - } catch (\Throwable $e) { |
41 | | - $this->error(' Failed: ' . $e->getMessage()); |
42 | | - return self::FAILURE; |
43 | 81 | } |
| 82 | + $this->newLine(); |
44 | 83 | } |
45 | 84 |
|
46 | | - $this->newLine(); |
47 | | - $this->info('Both certificates OK. Greek uses _greek template; Russian uses default template.'); |
| 85 | + if ($failed > 0) { |
| 86 | + $this->error("{$failed} test(s) failed."); |
| 87 | + return self::FAILURE; |
| 88 | + } |
| 89 | + if ($keepPdf) { |
| 90 | + $this->info('All test PDFs saved. Open: ' . $outDir); |
| 91 | + } else { |
| 92 | + $this->info('All test names passed.'); |
| 93 | + } |
48 | 94 | return self::SUCCESS; |
49 | 95 | } |
| 96 | + |
| 97 | + /** @return array<string>|null */ |
| 98 | + private function resolveTypes(string $typeOption): ?array |
| 99 | + { |
| 100 | + return match ($typeOption) { |
| 101 | + 'all' => ['excellence', 'super-organiser'], |
| 102 | + 'excellence' => ['excellence'], |
| 103 | + 'super-organiser', 'superorganiser' => ['super-organiser'], |
| 104 | + default => null, |
| 105 | + }; |
| 106 | + } |
50 | 107 | } |
0 commit comments