| Conditions | 8 |
| Paths | 8 |
| Total Lines | 53 |
| Code Lines | 30 |
| 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 |
||
| 66 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 67 | { |
||
| 68 | $tree = $request->getAttribute('tree'); |
||
| 69 | assert($tree instanceof Tree); |
||
| 70 | |||
| 71 | $data_filesystem = $request->getAttribute('filesystem.data'); |
||
| 72 | assert($data_filesystem instanceof FilesystemInterface); |
||
| 73 | |||
| 74 | $params = $request->getQueryParams(); |
||
| 75 | $xref = $params['xref']; |
||
| 76 | $fact_id = $params['fact_id']; |
||
| 77 | $media = Factory::media()->make($xref, $tree); |
||
| 78 | |||
| 79 | if ($media === null) { |
||
| 80 | return $this->media_file_service->replacementImage((string) StatusCodeInterface::STATUS_NOT_FOUND); |
||
| 81 | } |
||
| 82 | |||
| 83 | if (!$media->canShow()) { |
||
| 84 | return $this->media_file_service->replacementImage((string) StatusCodeInterface::STATUS_FORBIDDEN); |
||
| 85 | } |
||
| 86 | |||
| 87 | foreach ($media->mediaFiles() as $media_file) { |
||
| 88 | if ($media_file->factId() === $fact_id) { |
||
| 89 | if ($media_file->isExternal()) { |
||
| 90 | return redirect($media_file->filename()); |
||
| 91 | } |
||
| 92 | |||
| 93 | // Validate HTTP signature |
||
| 94 | $params['tree'] = $media_file->media()->tree()->name(); |
||
| 95 | |||
| 96 | try { |
||
| 97 | SignatureFactory::create(Site::getPreference('glide-key')) |
||
| 98 | ->validateRequest('', $params); |
||
| 99 | } catch (SignatureException $ex) { |
||
| 100 | return $this->media_file_service->replacementImage((string) StatusCodeInterface::STATUS_FORBIDDEN) |
||
| 101 | ->withHeader('X-Signature-Exception', $ex->getMessage()); |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($media_file->isImage()) { |
||
| 105 | $media_folder = $media_file->media()->tree()->getPreference('MEDIA_DIRECTORY', 'media/'); |
||
| 106 | $file = $media_file->filename(); |
||
| 107 | |||
| 108 | return $this->media_file_service->generateImage($media_folder, $file, $data_filesystem, $request->getQueryParams()); |
||
| 109 | } |
||
| 110 | |||
| 111 | // Shouldn't usually get here, as we only generate these URLs for images. |
||
| 112 | $extension = '.' . strtolower(pathinfo($media_file->filename(), PATHINFO_EXTENSION)); |
||
| 113 | |||
| 114 | return $this->media_file_service->replacementImage($extension); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return $this->media_file_service->replacementImage((string) StatusCodeInterface::STATUS_NOT_FOUND); |
||
| 119 | } |
||
| 121 |