| Conditions | 6 |
| Paths | 3 |
| Total Lines | 62 |
| Code Lines | 33 |
| 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 |
||
| 39 | public function __invoke(Request $request): Response |
||
| 40 | { |
||
| 41 | ini_set('max_execution_time', '300'); |
||
| 42 | ini_set('memory_limit', '512M'); |
||
| 43 | |||
| 44 | $data = json_decode($request->getContent(), true); |
||
| 45 | $documentIds = $data['ids'] ?? []; |
||
| 46 | |||
| 47 | if (empty($documentIds)) { |
||
| 48 | return new Response('No items selected.', Response::HTTP_BAD_REQUEST); |
||
| 49 | } |
||
| 50 | |||
| 51 | $documents = $this->documentRepo->findBy(['iid' => $documentIds]); |
||
| 52 | |||
| 53 | if (empty($documents)) { |
||
| 54 | return new Response('No documents found.', Response::HTTP_NOT_FOUND); |
||
| 55 | } |
||
| 56 | |||
| 57 | $zipName = 'selected_documents.zip'; |
||
| 58 | |||
| 59 | $response = new StreamedResponse( |
||
| 60 | function () use ($documents, $zipName): void { |
||
| 61 | // Creates a ZIP file containing the specified documents. |
||
| 62 | $options = new Archive(); |
||
| 63 | $options->setSendHttpHeaders(false); |
||
| 64 | $options->setContentType(self::CONTENT_TYPE); |
||
| 65 | |||
| 66 | $zip = new ZipStream($zipName, $options); |
||
| 67 | |||
| 68 | foreach ($documents as $document) { |
||
| 69 | $node = $document->getResourceNode(); |
||
| 70 | |||
| 71 | if (!$node) { |
||
| 72 | error_log('ResourceNode not found for document ID: '.$document->getIid()); |
||
| 73 | |||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->addNodeToZip($zip, $node); |
||
| 78 | } |
||
| 79 | |||
| 80 | if (0 === count($zip->files)) { |
||
| 81 | $zip->addFile('.empty', ''); |
||
| 82 | } |
||
| 83 | |||
| 84 | $zip->finish(); |
||
| 85 | }, |
||
| 86 | Response::HTTP_CREATED |
||
| 87 | ); |
||
| 88 | |||
| 89 | // Convert the file name to ASCII using iconv |
||
| 90 | $zipName = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $zipName); |
||
| 91 | |||
| 92 | $disposition = $response->headers->makeDisposition( |
||
| 93 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
| 94 | $zipName |
||
| 95 | ); |
||
| 96 | |||
| 97 | $response->headers->set('Content-Disposition', $disposition); |
||
| 98 | $response->headers->set('Content-Type', self::CONTENT_TYPE); |
||
| 99 | |||
| 100 | return $response; |
||
| 101 | } |
||
| 130 |