| Conditions | 9 |
| Paths | 11 |
| Total Lines | 56 |
| 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 |
||
| 66 | function httpGet(RequestInterface $request, ResponseInterface $response) { |
||
| 67 | |||
| 68 | $queryParams = $request->getQueryParameters(); |
||
| 69 | if (!array_key_exists('preview', $queryParams)) { |
||
| 70 | return true; |
||
| 71 | } |
||
| 72 | |||
| 73 | $path = $request->getPath(); |
||
| 74 | $node = $this->server->tree->getNodeForPath($path); |
||
| 75 | |||
| 76 | if (!$node instanceof IFileNode) { |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | $fileNode = $node->getNode(); |
||
| 80 | if (!$fileNode instanceof IPreviewNode) { |
||
| 81 | return false; |
||
| 82 | } |
||
| 83 | |||
| 84 | // Checking ACL, if available. |
||
| 85 | if ($aclPlugin = $this->server->getPlugin('acl')) { |
||
| 86 | /** @var \Sabre\DAVACL\Plugin $aclPlugin */ |
||
| 87 | $aclPlugin->checkPrivileges($path, '{DAV:}read'); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($image = $fileNode->getThumbnail($queryParams)) { |
||
| 91 | if ($image === null || !$image->valid()) { |
||
| 92 | throw new NotFound(); |
||
| 93 | } |
||
| 94 | $type = $image->mimeType(); |
||
| 95 | if (!in_array($type, ['image/png', 'image/jpeg', 'image/gif'])) { |
||
| 96 | $type = 'application/octet-stream'; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Enable output buffering |
||
| 100 | ob_start(); |
||
| 101 | // Capture the output |
||
| 102 | $image->show(); |
||
| 103 | $imageData = ob_get_contents(); |
||
| 104 | // Clear the output buffer |
||
| 105 | ob_end_clean(); |
||
| 106 | |||
| 107 | $response->setHeader('Content-Type', $type); |
||
| 108 | $response->setHeader('Content-Disposition', 'attachment'); |
||
| 109 | // cache 24h |
||
| 110 | $response->setHeader('Cache-Control', 'max-age=86400, must-revalidate'); |
||
| 111 | $response->setHeader('Expires', gmdate ("D, d M Y H:i:s", time() + 86400) . " GMT"); |
||
| 112 | |||
| 113 | $response->setStatus(200); |
||
| 114 | $response->setBody($imageData); |
||
| 115 | |||
| 116 | // Returning false to break the event chain |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | // TODO: add forceIcon handling .... if still needed |
||
| 120 | throw new NotFound(); |
||
| 121 | } |
||
| 122 | } |
||
| 123 |