|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\CertificateExcellence; |
| 6 | +use App\Excellence; |
| 7 | +use App\User; |
| 8 | +use Illuminate\Console\Command; |
| 9 | + |
| 10 | +/** |
| 11 | + * Regenerate a single certificate PDF and overwrite the existing file on S3 |
| 12 | + * so the same URL serves the corrected PDF. No email sent; user does not need to notice. |
| 13 | + * |
| 14 | + * Use when the PDF shows the wrong year (e.g. 2024 instead of 2023) and you want |
| 15 | + * to fix it without changing the link or notifying the recipient. |
| 16 | + */ |
| 17 | +class CertificateRegenerateInPlace extends Command |
| 18 | +{ |
| 19 | + protected $signature = 'certificate:regenerate-in-place |
| 20 | + {--email= : User email (certificate owner)} |
| 21 | + {--certificate-url= : Exact certificate URL to replace (if user has multiple certs)} |
| 22 | + {--edition= : Year to show on the certificate (e.g. 2023); default: use value from DB} |
| 23 | + {--dry-run : Show what would be done, do not regenerate}'; |
| 24 | + |
| 25 | + protected $description = 'Regenerate one certificate PDF in place (same URL); no email sent. Use to fix wrong year or content.'; |
| 26 | + |
| 27 | + public function handle(): int |
| 28 | + { |
| 29 | + $email = trim((string) $this->option('email')); |
| 30 | + $certificateUrl = trim((string) $this->option('certificate-url')); |
| 31 | + $editionOverride = $this->option('edition'); |
| 32 | + $dryRun = (bool) $this->option('dry-run'); |
| 33 | + |
| 34 | + if ($email === '') { |
| 35 | + $this->error('Provide --email (certificate owner).'); |
| 36 | + return self::FAILURE; |
| 37 | + } |
| 38 | + |
| 39 | + $user = User::where('email', $email)->first(); |
| 40 | + if (! $user) { |
| 41 | + $this->error("User not found: {$email}"); |
| 42 | + return self::FAILURE; |
| 43 | + } |
| 44 | + |
| 45 | + $excellence = null; |
| 46 | + if ($certificateUrl !== '') { |
| 47 | + $excellence = Excellence::where('user_id', $user->id) |
| 48 | + ->where(function ($q) use ($certificateUrl) { |
| 49 | + $q->where('certificate_url', $certificateUrl) |
| 50 | + ->orWhere('certificate_url', 'like', '%' . basename(parse_url($certificateUrl, PHP_URL_PATH)) . '%'); |
| 51 | + }) |
| 52 | + ->first(); |
| 53 | + if (! $excellence) { |
| 54 | + $this->error('No excellence record found for this user with that certificate URL.'); |
| 55 | + return self::FAILURE; |
| 56 | + } |
| 57 | + } else { |
| 58 | + $candidates = Excellence::where('user_id', $user->id) |
| 59 | + ->whereNotNull('certificate_url') |
| 60 | + ->orderBy('edition') |
| 61 | + ->orderBy('type') |
| 62 | + ->get(); |
| 63 | + if ($candidates->isEmpty()) { |
| 64 | + $this->error("No certificate(s) found for {$email}."); |
| 65 | + return self::FAILURE; |
| 66 | + } |
| 67 | + if ($candidates->count() > 1) { |
| 68 | + $this->error('User has more than one certificate. Specify --certificate-url= to choose which one to regenerate.'); |
| 69 | + $this->table( |
| 70 | + ['id', 'edition', 'type', 'certificate_url'], |
| 71 | + $candidates->map(fn (Excellence $e) => [$e->id, $e->edition, $e->type, $e->certificate_url])->toArray() |
| 72 | + ); |
| 73 | + return self::FAILURE; |
| 74 | + } |
| 75 | + $excellence = $candidates->first(); |
| 76 | + } |
| 77 | + |
| 78 | + $excellence->load('user'); |
| 79 | + $edition = $editionOverride !== null && $editionOverride !== '' |
| 80 | + ? (int) $editionOverride |
| 81 | + : (int) $excellence->edition; |
| 82 | + $name = $excellence->name_for_certificate ?? trim(($excellence->user->firstname ?? '') . ' ' . ($excellence->user->lastname ?? '')); |
| 83 | + $name = $name !== '' ? $name : 'Unknown'; |
| 84 | + $type = $excellence->type === 'SuperOrganiser' ? 'super-organiser' : 'excellence'; |
| 85 | + $numberOfActivities = $type === 'super-organiser' ? (int) $excellence->user->activities($edition) : 0; |
| 86 | + |
| 87 | + $this->info("Certificate: excellence id {$excellence->id}, edition {$excellence->edition} (will generate with year {$edition}), type {$excellence->type}."); |
| 88 | + $this->line('URL (unchanged): ' . $excellence->certificate_url); |
| 89 | + |
| 90 | + if ($dryRun) { |
| 91 | + $this->warn('Dry run: no regeneration. Run without --dry-run to overwrite the PDF on S3.'); |
| 92 | + return self::SUCCESS; |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + $cert = new CertificateExcellence( |
| 97 | + $edition, |
| 98 | + $name, |
| 99 | + $type, |
| 100 | + $numberOfActivities, |
| 101 | + (int) $excellence->user->id, |
| 102 | + (string) ($excellence->user->email ?? '') |
| 103 | + ); |
| 104 | + $url = $cert->generateReplacing($excellence->certificate_url); |
| 105 | + $excellence->update([ |
| 106 | + 'certificate_url' => $url, |
| 107 | + 'certificate_generation_error' => null, |
| 108 | + ]); |
| 109 | + $this->info('Done. PDF regenerated; same link now serves the corrected certificate. No email sent.'); |
| 110 | + } catch (\Throwable $e) { |
| 111 | + $this->error('Regeneration failed: ' . $e->getMessage()); |
| 112 | + $excellence->update(['certificate_generation_error' => $e->getMessage()]); |
| 113 | + |
| 114 | + return self::FAILURE; |
| 115 | + } |
| 116 | + |
| 117 | + return self::SUCCESS; |
| 118 | + } |
| 119 | +} |
0 commit comments