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