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