| Conditions | 13 |
| Paths | 8 |
| Total Lines | 87 |
| Code Lines | 48 |
| 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 |
||
| 48 | public function __invoke(Request $request): Response |
||
| 49 | { |
||
| 50 | ini_set('max_execution_time', '300'); |
||
| 51 | ini_set('memory_limit', '512M'); |
||
| 52 | |||
| 53 | $data = json_decode($request->getContent(), true); |
||
| 54 | $documentIds = $data['ids'] ?? []; |
||
| 55 | |||
| 56 | if (empty($documentIds)) { |
||
| 57 | return new Response('No items selected.', Response::HTTP_BAD_REQUEST); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($this->security->isGranted('ROLE_ADMIN')) { |
||
| 61 | $documents = $this->documentRepo->findBy(['iid' => $documentIds]); |
||
| 62 | } else { |
||
| 63 | $course = $this->cidReqHelper->getCourseEntity(); |
||
| 64 | $session = $this->cidReqHelper->getSessionEntity(); |
||
| 65 | $group = $this->cidReqHelper->getGroupEntity(); |
||
| 66 | |||
| 67 | if (!$course || !$this->security->isGranted(CourseVoter::VIEW, $course)) { |
||
| 68 | throw new NotAllowedException("You're not allowed in this course"); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($session && !$this->security->isGranted(SessionVoter::VIEW, $session)) { |
||
| 72 | throw new NotAllowedException("You're not allowed in this session"); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($group && !$this->security->isGranted(GroupVoter::VIEW, $group)) { |
||
| 76 | throw new NotAllowedException("You're not allowed in this group"); |
||
| 77 | } |
||
| 78 | |||
| 79 | $qb = $this->documentRepo->getResourcesByCourse($course, $session, $group); |
||
| 80 | $qb->andWhere( |
||
| 81 | $qb->expr()->in('resource.iid', $documentIds) |
||
| 82 | ); |
||
| 83 | |||
| 84 | $documents = $qb->getQuery()->getResult(); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (empty($documents)) { |
||
| 88 | return new Response('No documents found.', Response::HTTP_NOT_FOUND); |
||
| 89 | } |
||
| 90 | |||
| 91 | $zipName = 'selected_documents.zip'; |
||
| 92 | |||
| 93 | $response = new StreamedResponse( |
||
| 94 | function () use ($documents, $zipName): void { |
||
| 95 | // Creates a ZIP file containing the specified documents. |
||
| 96 | $options = new Archive(); |
||
| 97 | $options->setSendHttpHeaders(false); |
||
| 98 | $options->setContentType(self::CONTENT_TYPE); |
||
| 99 | |||
| 100 | $zip = new ZipStream($zipName, $options); |
||
| 101 | |||
| 102 | foreach ($documents as $document) { |
||
| 103 | $node = $document->getResourceNode(); |
||
| 104 | |||
| 105 | if (!$node) { |
||
| 106 | error_log('ResourceNode not found for document ID: '.$document->getIid()); |
||
| 107 | |||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | $this->addNodeToZip($zip, $node); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (0 === count($zip->files)) { |
||
| 115 | $zip->addFile('.empty', ''); |
||
| 116 | } |
||
| 117 | |||
| 118 | $zip->finish(); |
||
| 119 | }, |
||
| 120 | Response::HTTP_CREATED |
||
| 121 | ); |
||
| 122 | |||
| 123 | // Convert the file name to ASCII using iconv |
||
| 124 | $zipName = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $zipName); |
||
| 125 | |||
| 126 | $disposition = $response->headers->makeDisposition( |
||
| 127 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
| 128 | $zipName |
||
| 129 | ); |
||
| 130 | |||
| 131 | $response->headers->set('Content-Disposition', $disposition); |
||
| 132 | $response->headers->set('Content-Type', self::CONTENT_TYPE); |
||
| 133 | |||
| 134 | return $response; |
||
| 135 | } |
||
| 168 |