| Conditions | 14 |
| Paths | 225 |
| Total Lines | 70 |
| Code Lines | 42 |
| 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 |
||
| 85 | public function httpGet(RequestInterface $request, ResponseInterface $response) { |
||
| 86 | $queryParams = $request->getQueryParameters(); |
||
| 87 | if (!\array_key_exists('preview', $queryParams)) { |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | |||
| 91 | $path = $request->getPath(); |
||
| 92 | $node = $this->server->tree->getNodeForPath($path); |
||
| 93 | |||
| 94 | if (!$node instanceof IFileNode) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | $fileNode = $node->getNode(); |
||
| 98 | if (!$fileNode instanceof IPreviewNode) { |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Checking ACL, if available. |
||
| 103 | if ($aclPlugin = $this->server->getPlugin('acl')) { |
||
| 104 | /** @var \Sabre\DAVACL\Plugin $aclPlugin */ |
||
| 105 | $aclPlugin->checkPrivileges($path, '{DAV:}read'); |
||
| 106 | } |
||
| 107 | |||
| 108 | try { |
||
| 109 | if (!$this->previewManager->isAvailable($fileNode)) { |
||
|
|
|||
| 110 | // no preview available or preview disabled |
||
| 111 | throw new NotFound(); |
||
| 112 | } |
||
| 113 | if ($image = $fileNode->getThumbnail($queryParams)) { |
||
| 114 | if ($image === null || !$image->valid()) { |
||
| 115 | throw new NotFound(); |
||
| 116 | } |
||
| 117 | $type = $image->mimeType(); |
||
| 118 | if (!\in_array($type, ['image/png', 'image/jpeg', 'image/gif'])) { |
||
| 119 | $type = 'application/octet-stream'; |
||
| 120 | } |
||
| 121 | |||
| 122 | // Enable output buffering |
||
| 123 | \ob_start(); |
||
| 124 | // Capture the output |
||
| 125 | $image->show(); |
||
| 126 | $imageData = \ob_get_contents(); |
||
| 127 | // Clear the output buffer |
||
| 128 | \ob_end_clean(); |
||
| 129 | |||
| 130 | $response->setHeader('Content-Type', $type); |
||
| 131 | $response->setHeader('Content-Disposition', 'attachment'); |
||
| 132 | // cache 24h |
||
| 133 | $response->setHeader('Cache-Control', 'max-age=86400, must-revalidate'); |
||
| 134 | $response->setHeader('Expires', \gmdate("D, d M Y H:i:s", $this->timeFactory->getTime() + 86400) . " GMT"); |
||
| 135 | |||
| 136 | $response->setStatus(200); |
||
| 137 | $response->setBody($imageData); |
||
| 138 | |||
| 139 | // Returning false to break the event chain |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | } catch (GenericEncryptionException $e) { |
||
| 143 | // returning 403 because some apps stops syncing if 503 is returned. |
||
| 144 | throw new Forbidden('Encryption not ready: ' . $e->getMessage()); |
||
| 145 | } catch (StorageNotAvailableException $e) { |
||
| 146 | throw new ServiceUnavailable('Failed to open file: ' . $e->getMessage()); |
||
| 147 | } catch (ForbiddenException $ex) { |
||
| 148 | throw new Forbidden($ex->getMessage(), $ex->getRetry()); |
||
| 149 | } catch (LockedException $e) { |
||
| 150 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 151 | } |
||
| 152 | // TODO: add forceIcon handling .... if still needed |
||
| 153 | throw new NotFound(); |
||
| 154 | } |
||
| 155 | } |
||
| 156 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: