| Conditions | 17 |
| Paths | 777 |
| Total Lines | 51 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 32 | #[Route('/{hash}.html', name: 'chamilo_certificate_public_view', methods: ['GET'])] |
||
| 33 | public function view(string $hash): Response |
||
| 34 | { |
||
| 35 | // Build the expected certificate filename from the hash |
||
| 36 | $filename = $hash.'.html'; |
||
| 37 | $candidates = [$filename, '/'.$filename, $hash, '/'.$hash]; |
||
| 38 | |||
| 39 | $certificate = null; |
||
| 40 | $matchedPath = ''; |
||
| 41 | foreach ($candidates as $cand) { |
||
| 42 | $row = $this->certificateRepository->findOneBy(['pathCertificate' => $cand]); |
||
| 43 | if ($row) { $certificate = $row; $matchedPath = $cand; break; } |
||
| 44 | } |
||
| 45 | if (!$certificate) { |
||
| 46 | throw new NotFoundHttpException('The requested certificate does not exist.'); |
||
| 47 | } |
||
| 48 | |||
| 49 | // Check if public access is globally allowed and certificate is marked as published |
||
| 50 | $allowPublic = 'true' === $this->settingsManager->getSetting('course.allow_public_certificates', true); |
||
| 51 | $allowSessionAdmin = 'true' === $this->settingsManager->getSetting('certificate.session_admin_can_download_all_certificates', true); |
||
| 52 | $user = $this->userHelper->getCurrent(); |
||
| 53 | $securityUser = $this->getUser(); |
||
| 54 | $isOwner = $securityUser && method_exists($securityUser, 'getId') && $user->getId() === $securityUser->getId(); |
||
| 55 | $isPlatformAdmin = method_exists($user, 'isAdmin') && $user->isAdmin(); |
||
| 56 | |||
| 57 | if (!$isOwner && !$isPlatformAdmin) { |
||
| 58 | $isPublic = ($allowPublic && $certificate->getPublish()); |
||
| 59 | $isSessAdminAllowed = ($allowSessionAdmin && method_exists($user, 'isSessionAdmin') && $user->isSessionAdmin()); |
||
| 60 | if (!$isPublic && !$isSessAdminAllowed) { |
||
| 61 | throw new AccessDeniedHttpException('The requested certificate is not public.'); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | // Fetch the actual certificate file from personal files using its title |
||
| 66 | $personalFileRepo = Container::getPersonalFileRepository(); |
||
| 67 | $pf = null; |
||
| 68 | $pfMatch = ''; |
||
| 69 | foreach ($candidates as $cand) { |
||
| 70 | $row = $personalFileRepo->findOneBy(['title' => $cand]); |
||
| 71 | if ($row) { $pf = $row; $pfMatch = $cand; break; } |
||
| 72 | } |
||
| 73 | if (!$pf) { |
||
| 74 | throw new NotFoundHttpException('The certificate file was not found.'); |
||
| 75 | } |
||
| 76 | |||
| 77 | $content = $personalFileRepo->getResourceFileContent($pf); |
||
| 78 | $content = str_replace(' media="screen"', '', $content); |
||
| 79 | |||
| 80 | // Return the certificate as a raw HTML response |
||
| 81 | return new Response('<!DOCTYPE html>'.$content, 200, [ |
||
| 82 | 'Content-Type' => 'text/html; charset=UTF-8', |
||
| 83 | ]); |
||
| 151 |