| Conditions | 18 |
| Paths | 1911 |
| Total Lines | 62 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 86 | #[Route('/{hash}.pdf', name: 'chamilo_certificate_public_pdf', methods: ['GET'])] |
||
| 87 | public function downloadPdf(string $hash): Response |
||
| 88 | { |
||
| 89 | $filename = $hash.'.html'; |
||
| 90 | $candidates = [$filename, '/'.$filename, $hash, '/'.$hash]; |
||
| 91 | |||
| 92 | $certificate = null; |
||
| 93 | $matchedPath = ''; |
||
| 94 | foreach ($candidates as $cand) { |
||
| 95 | $row = $this->certificateRepository->findOneBy(['pathCertificate' => $cand]); |
||
| 96 | if ($row) { $certificate = $row; $matchedPath = $cand; break; } |
||
| 97 | } |
||
| 98 | if (!$certificate) { |
||
| 99 | throw $this->createNotFoundException('The requested certificate does not exist.'); |
||
| 100 | } |
||
| 101 | |||
| 102 | $allowPublic = 'true' === $this->settingsManager->getSetting('course.allow_public_certificates', true); |
||
| 103 | $allowSessionAdmin = 'true' === $this->settingsManager->getSetting('certificate.session_admin_can_download_all_certificates', true); |
||
| 104 | $user = $this->userHelper->getCurrent(); |
||
| 105 | $securityUser = $this->getUser(); |
||
| 106 | $isOwner = $securityUser && method_exists($securityUser, 'getId') && $user->getId() === $securityUser->getId(); |
||
| 107 | $isPlatformAdmin = method_exists($user, 'isAdmin') && $user->isAdmin(); |
||
| 108 | |||
| 109 | if (!$isOwner && !$isPlatformAdmin) { |
||
| 110 | $isPublic = ($allowPublic && $certificate->getPublish()); |
||
| 111 | $isSessAdminAllowed = ($allowSessionAdmin && method_exists($user, 'isSessionAdmin') && $user->isSessionAdmin()); |
||
| 112 | if (!$isPublic && !$isSessAdminAllowed) { |
||
| 113 | throw new AccessDeniedHttpException('The requested certificate is not public.'); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $personalFileRepo = Container::getPersonalFileRepository(); |
||
| 118 | $pf = null; |
||
| 119 | $pfMatch = ''; |
||
| 120 | foreach ($candidates as $cand) { |
||
| 121 | $row = $personalFileRepo->findOneBy(['title' => $cand]); |
||
| 122 | if ($row) { $pf = $row; $pfMatch = $cand; break; } |
||
| 123 | } |
||
| 124 | if (!$pf) { |
||
| 125 | throw $this->createNotFoundException('The certificate file was not found.'); |
||
| 126 | } |
||
| 127 | |||
| 128 | $html = $personalFileRepo->getResourceFileContent($pf); |
||
| 129 | $html = str_replace(' media="screen"', '', $html); |
||
| 130 | |||
| 131 | try { |
||
| 132 | $mpdf = new Mpdf([ |
||
| 133 | 'format' => 'A4', |
||
| 134 | 'tempDir' => api_get_path(SYS_ARCHIVE_PATH).'mpdf/', |
||
| 135 | ]); |
||
| 136 | $mpdf->WriteHTML($html); |
||
| 137 | |||
| 138 | return new Response( |
||
| 139 | $mpdf->Output('certificate.pdf', Destination::DOWNLOAD), |
||
| 140 | 200, |
||
| 141 | [ |
||
| 142 | 'Content-Type' => 'application/pdf', |
||
| 143 | 'Content-Disposition' => 'attachment; filename="certificate.pdf"', |
||
| 144 | ] |
||
| 145 | ); |
||
| 146 | } catch (MpdfException $e) { |
||
| 147 | throw new RuntimeException('Failed to generate PDF: '.$e->getMessage(), 500, $e); |
||
| 148 | } |
||
| 151 |