| Total Complexity | 44 |
| Total Lines | 229 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CertificateController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CertificateController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | #[Route('/certificates')] |
||
| 26 | class CertificateController extends AbstractController |
||
| 27 | { |
||
| 28 | public function __construct( |
||
| 29 | private readonly GradebookCertificateRepository $certificateRepository, |
||
| 30 | private readonly SettingsManager $settingsManager, |
||
| 31 | private readonly UserHelper $userHelper, |
||
| 32 | private readonly ResourceNodeRepository $resourceNodeRepository, |
||
| 33 | ) {} |
||
| 34 | |||
| 35 | #[Route('/{hash}.html', name: 'chamilo_certificate_public_view', methods: ['GET'])] |
||
| 36 | public function view(string $hash): Response |
||
| 37 | { |
||
| 38 | // Resolve certificate row (keeps legacy path logic working) |
||
| 39 | [$certificate] = $this->resolveCertificateByHash($hash); |
||
| 40 | |||
| 41 | // Permission checks |
||
| 42 | $this->assertCertificateAccess($certificate); |
||
| 43 | |||
| 44 | // Read HTML from resource storage (new) or personal-file (legacy) |
||
| 45 | $html = $this->readCertificateHtml($certificate, $hash); |
||
| 46 | $html = str_replace(' media="screen"', '', $html); |
||
| 47 | |||
| 48 | return new Response('<!DOCTYPE html>'.$html, 200, [ |
||
| 49 | 'Content-Type' => 'text/html; charset=UTF-8', |
||
| 50 | ]); |
||
| 51 | } |
||
| 52 | |||
| 53 | #[Route('/{hash}.pdf', name: 'chamilo_certificate_public_pdf', methods: ['GET'])] |
||
| 54 | public function downloadPdf(string $hash): Response |
||
| 55 | { |
||
| 56 | // Resolve certificate row |
||
| 57 | [$certificate] = $this->resolveCertificateByHash($hash); |
||
| 58 | |||
| 59 | // Permission checks |
||
| 60 | $this->assertCertificateAccess($certificate); |
||
| 61 | |||
| 62 | // Read HTML and render PDF |
||
| 63 | $html = $this->readCertificateHtml($certificate, $hash); |
||
| 64 | $html = str_replace(' media="screen"', '', $html); |
||
| 65 | |||
| 66 | try { |
||
| 67 | $mpdf = new Mpdf([ |
||
| 68 | 'format' => 'A4', |
||
| 69 | 'tempDir' => api_get_path(SYS_ARCHIVE_PATH).'mpdf/', |
||
| 70 | ]); |
||
| 71 | $mpdf->WriteHTML($html); |
||
| 72 | $pdfBinary = $mpdf->Output('', Destination::STRING_RETURN); |
||
| 73 | |||
| 74 | return new Response( |
||
| 75 | $pdfBinary, |
||
| 76 | 200, |
||
| 77 | [ |
||
| 78 | 'Content-Type' => 'application/pdf', |
||
| 79 | 'Content-Disposition' => 'attachment; filename="certificate.pdf"', |
||
| 80 | ] |
||
| 81 | ); |
||
| 82 | } catch (MpdfException $e) { |
||
| 83 | throw new RuntimeException('Failed to generate PDF: '.$e->getMessage(), 500, $e); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Resolve the certificate row via path. |
||
| 89 | * |
||
| 90 | * @return array{0: GradebookCertificate, 1: string} |
||
| 91 | * |
||
| 92 | * @throws NotFoundHttpException |
||
| 93 | */ |
||
| 94 | private function resolveCertificateByHash(string $hash): array |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Owner/admin OR (public+published) OR (session admin if allowed). |
||
| 120 | * |
||
| 121 | * @throws AccessDeniedHttpException |
||
| 122 | */ |
||
| 123 | private function assertCertificateAccess(GradebookCertificate $certificate): void |
||
| 124 | { |
||
| 125 | $allowPublic = 'true' === $this->settingsManager->getSetting('certificate.allow_public_certificates', true); |
||
| 126 | $allowSessionAdmin = 'true' === $this->settingsManager->getSetting('certificate.session_admin_can_download_all_certificates', true); |
||
| 127 | |||
| 128 | $user = $this->userHelper->getCurrent(); |
||
| 129 | $securityUser = $this->getUser(); |
||
| 130 | |||
| 131 | $isOwner = $securityUser && method_exists($securityUser, 'getId') && $user->getId() === $securityUser->getId(); |
||
| 132 | $isPlatformAdmin = method_exists($user, 'isAdmin') && $user->isAdmin(); |
||
| 133 | |||
| 134 | if ($isOwner || $isPlatformAdmin) { |
||
| 135 | return; |
||
| 136 | } |
||
| 137 | |||
| 138 | $isPublic = ($allowPublic && $certificate->getPublish()); |
||
| 139 | $isSessAdminAllowed = ($allowSessionAdmin && method_exists($user, 'isSessionAdmin') && $user->isSessionAdmin()); |
||
| 140 | |||
| 141 | if (!$isPublic && !$isSessAdminAllowed) { |
||
| 142 | throw new AccessDeniedHttpException('The requested certificate is not public.'); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns certificate HTML from resource-node (new flow) or personal file (legacy). |
||
| 148 | * |
||
| 149 | * It tries multiple physical paths to accommodate different storage layouts: |
||
| 150 | * 1) node->getPath() + ResourceFile->title |
||
| 151 | * 2) node->getPath() + ResourceFile->original_name |
||
| 152 | * 3) sharded path "resource/<a>/<b>/<c>/<file>" using title |
||
| 153 | * 4) sharded path "resource/<a>/<b>/<c>/<file>" using original_name |
||
| 154 | * 5) final fallback: generic getResourceNodeFileContent() |
||
| 155 | * 6) legacy fallback: PersonalFile by title |
||
| 156 | * |
||
| 157 | * @throws NotFoundHttpException |
||
| 158 | */ |
||
| 159 | private function readCertificateHtml(GradebookCertificate $certificate, string $hash): string |
||
| 254 | } |
||
| 255 | } |
||
| 256 |