Conditions | 23 |
Paths | 1 |
Total Lines | 110 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 16 | ||
Bugs | 2 | Features | 1 |
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 |
||
110 | public function __invoke(callable $handler) |
||
111 | { |
||
112 | return function (RequestInterface $request, array $options) use (&$handler) { |
||
113 | if (!isset($this->httpMethods[strtoupper($request->getMethod())])) { |
||
114 | // No caching for this method allowed |
||
115 | return $handler($request, $options)->then( |
||
116 | function (ResponseInterface $response) { |
||
117 | return $response->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_MISS); |
||
118 | } |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | if ($request->hasHeader(self::HEADER_RE_VALIDATION)) { |
||
123 | // It's a re-validation request, so bypass the cache! |
||
124 | return $handler($request->withoutHeader(self::HEADER_RE_VALIDATION), $options); |
||
125 | } |
||
126 | |||
127 | // Retrieve information from request (Cache-Control) |
||
128 | $reqCacheControl = new KeyValueHttpHeader($request->getHeader('Cache-Control')); |
||
129 | $onlyFromCache = $reqCacheControl->has('only-if-cached'); |
||
130 | $staleResponse = $reqCacheControl->has('max-stale') |
||
131 | && $reqCacheControl->get('max-stale') === ''; |
||
132 | $maxStaleCache = $reqCacheControl->get('max-stale', null); |
||
133 | $minFreshCache = $reqCacheControl->get('min-fresh', null); |
||
134 | |||
135 | // If cache => return new FulfilledPromise(...) with response |
||
136 | $cacheEntry = $this->cacheStorage->fetch($request); |
||
137 | if ($cacheEntry instanceof CacheEntry) { |
||
138 | $body = $cacheEntry->getResponse()->getBody(); |
||
139 | if ($body->tell() > 0) { |
||
140 | $body->rewind(); |
||
141 | } |
||
142 | |||
143 | if ($cacheEntry->isFresh() |
||
144 | && ($minFreshCache === null || $cacheEntry->getStaleAge() + (int)$minFreshCache <= 0) |
||
145 | ) { |
||
146 | // Cache HIT! |
||
147 | return new FulfilledPromise( |
||
148 | $cacheEntry->getResponse()->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT) |
||
149 | ); |
||
150 | } elseif ($staleResponse |
||
151 | || ($maxStaleCache !== null && $cacheEntry->getStaleAge() <= $maxStaleCache) |
||
152 | ) { |
||
153 | // Staled cache! |
||
154 | return new FulfilledPromise( |
||
155 | $cacheEntry->getResponse()->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT) |
||
156 | ); |
||
157 | } elseif ($cacheEntry->hasValidationInformation() && !$onlyFromCache) { |
||
158 | // Re-validation header |
||
159 | $request = static::getRequestWithReValidationHeader($request, $cacheEntry); |
||
160 | |||
161 | if ($cacheEntry->staleWhileValidate()) { |
||
162 | static::addReValidationRequest($request, $this->cacheStorage, $cacheEntry); |
||
163 | |||
164 | return new FulfilledPromise( |
||
165 | $cacheEntry->getResponse() |
||
166 | ->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_STALE) |
||
167 | ); |
||
168 | } |
||
169 | } |
||
170 | } else { |
||
171 | $cacheEntry = null; |
||
172 | } |
||
173 | |||
174 | if ($cacheEntry === null && $onlyFromCache) { |
||
175 | // Explicit asking of a cached response => 504 |
||
176 | return new FulfilledPromise( |
||
177 | new Response(504) |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | /** @var Promise $promise */ |
||
182 | $promise = $handler($request, $options); |
||
183 | |||
184 | return $promise->then( |
||
185 | function (ResponseInterface $response) use ($request, $cacheEntry) { |
||
186 | // Check if error and looking for a staled content |
||
187 | if ($response->getStatusCode() >= 500) { |
||
188 | $responseStale = static::getStaleResponse($cacheEntry); |
||
189 | if ($responseStale instanceof ResponseInterface) { |
||
190 | return $responseStale; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | if ($response->getStatusCode() == 304 && $cacheEntry instanceof CacheEntry) { |
||
195 | // Not modified => cache entry is re-validate |
||
196 | /** @var ResponseInterface $response */ |
||
197 | $response = $response |
||
198 | ->withStatus($cacheEntry->getResponse()->getStatusCode()) |
||
199 | ->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT); |
||
200 | $response = $response->withBody($cacheEntry->getResponse()->getBody()); |
||
201 | } else { |
||
202 | $response = $response->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_MISS); |
||
203 | } |
||
204 | |||
205 | return self::addToCache($this->cacheStorage, $request, $response); |
||
206 | }, |
||
207 | function (\Exception $ex) use ($cacheEntry) { |
||
208 | if ($ex instanceof TransferException) { |
||
209 | $response = static::getStaleResponse($cacheEntry); |
||
210 | if ($response instanceof ResponseInterface) { |
||
211 | return $response; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | throw $ex; |
||
216 | } |
||
217 | ); |
||
218 | }; |
||
219 | } |
||
220 | |||
331 |