| Conditions | 13 |
| Paths | 20 |
| Total Lines | 64 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 2 | Features | 4 |
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 |
||
| 109 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
| 110 | { |
||
| 111 | if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { |
||
| 112 | throw new RuntimeException('ResponsiveImage middleware needs FormatNegotiator executed before'); |
||
| 113 | } |
||
| 114 | |||
| 115 | $format = FormatNegotiator::getFormat($request); |
||
| 116 | |||
| 117 | switch ($format) { |
||
| 118 | case 'jpg': |
||
| 119 | case 'jpeg': |
||
| 120 | case 'gif': |
||
| 121 | case 'png': |
||
| 122 | $key = $this->getCacheKey($request); |
||
| 123 | |||
| 124 | //Get from the cache |
||
| 125 | if ($cached = $this->getFromCache($key, $response)) { |
||
| 126 | return $cached; |
||
| 127 | } |
||
| 128 | |||
| 129 | $info = $this->parsePath($request->getUri()->getPath()); |
||
| 130 | |||
| 131 | if (!$info) { |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | |||
| 135 | //Removes the transform info in the path |
||
| 136 | list($path, $transform) = $info; |
||
| 137 | $request = $request->withUri($request->getUri()->withPath($path)); |
||
| 138 | $response = $next($request, $response); |
||
| 139 | |||
| 140 | //Transform |
||
| 141 | if ($response->getStatusCode() === 200 && $response->getBody()->getSize()) { |
||
| 142 | $response = $this->transform($request, $response, $transform); |
||
| 143 | |||
| 144 | //Save in the cache |
||
| 145 | $this->saveIntoCache($key, $response); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $response; |
||
| 149 | |||
| 150 | case 'html': |
||
| 151 | $generator = function ($path, $transform) use ($request) { |
||
| 152 | $info = pathinfo($path); |
||
| 153 | |||
| 154 | if (!isset($this->sizes[$transform])) { |
||
| 155 | throw new \InvalidArgumentException(sprintf('The image size "%s" is not valid', $transform)); |
||
| 156 | } |
||
| 157 | |||
| 158 | return Utils\Helpers::joinPath($info['dirname'], $transform.$info['basename']); |
||
| 159 | }; |
||
| 160 | |||
| 161 | $request = Middleware::setAttribute($request, self::KEY_GENERATOR, $generator); |
||
| 162 | $response = $next($request, $response); |
||
| 163 | |||
| 164 | if (!empty($this->clientHints)) { |
||
| 165 | return $response->withHeader('Accept-CH', implode(',', $this->clientHints)); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $response; |
||
| 169 | } |
||
| 170 | |||
| 171 | return $next($request, $response); |
||
| 172 | } |
||
| 173 | |||
| 276 |