These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Ivory Http Adapter package. |
||
5 | * |
||
6 | * (c) Eric GELOEN <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please read the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Ivory\HttpAdapter\Event\Cache; |
||
13 | |||
14 | use Ivory\HttpAdapter\Event\Formatter\Formatter; |
||
15 | use Ivory\HttpAdapter\Event\Formatter\FormatterInterface; |
||
16 | use Ivory\HttpAdapter\HttpAdapterException; |
||
17 | use Ivory\HttpAdapter\Message\InternalRequestInterface; |
||
18 | use Ivory\HttpAdapter\Message\MessageFactoryInterface; |
||
19 | use Ivory\HttpAdapter\Message\ResponseInterface; |
||
20 | use Ivory\HttpAdapter\Event\Cache\Adapter\CacheAdapterInterface; |
||
21 | |||
22 | /** |
||
23 | * Cache. |
||
24 | * |
||
25 | * @author GeLo <[email protected]> |
||
26 | */ |
||
27 | class Cache implements CacheInterface |
||
28 | { |
||
29 | /** @var \Ivory\HttpAdapter\Event\Cache\Adapter\CacheAdapterInterface */ |
||
30 | private $adapter; |
||
31 | |||
32 | /** @var \Ivory\HttpAdapter\Event\Formatter\FormatterInterface */ |
||
33 | private $formatter; |
||
34 | |||
35 | /** @var integer|null */ |
||
36 | private $lifetime; |
||
37 | |||
38 | /** @var boolean */ |
||
39 | private $cacheException; |
||
40 | |||
41 | /** |
||
42 | * Creates a cache. |
||
43 | * |
||
44 | * @param \Ivory\HttpAdapter\Event\Cache\Adapter\CacheAdapterInterface $adapter The adapter. |
||
45 | * @param \Ivory\HttpAdapter\Event\Formatter\FormatterInterface|null $formatter The formatter. |
||
46 | * @param integer|null $lifetime The lifetime. |
||
47 | * @param boolean $cacheException TRUE if the exceptions should be cached else FALSE. |
||
48 | */ |
||
49 | 285 | public function __construct( |
|
50 | CacheAdapterInterface $adapter, |
||
51 | FormatterInterface $formatter = null, |
||
52 | $lifetime = null, |
||
53 | $cacheException = true |
||
54 | ) { |
||
55 | 285 | $this->setAdapter($adapter); |
|
56 | 285 | $this->setFormatter($formatter ?: new Formatter()); |
|
57 | 285 | $this->setlifetime($lifetime); |
|
58 | 285 | $this->cacheException($cacheException); |
|
59 | 285 | } |
|
60 | |||
61 | /** |
||
62 | * Gets the adapter. |
||
63 | * |
||
64 | * @return \Ivory\HttpAdapter\Event\Cache\Adapter\CacheAdapterInterface The adapter. |
||
65 | */ |
||
66 | 38 | public function getAdapter() |
|
67 | { |
||
68 | 38 | return $this->adapter; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Sets the adapter. |
||
73 | * |
||
74 | * @param \Ivory\HttpAdapter\Event\Cache\Adapter\CacheAdapterInterface $adapter The adapter. |
||
75 | */ |
||
76 | 285 | public function setAdapter(CacheAdapterInterface $adapter) |
|
77 | { |
||
78 | 285 | $this->adapter = $adapter; |
|
79 | 285 | } |
|
80 | |||
81 | /** |
||
82 | * Gets the formatter. |
||
83 | * |
||
84 | * @return \Ivory\HttpAdapter\Event\Formatter\FormatterInterface The formatter. |
||
85 | */ |
||
86 | 38 | public function getFormatter() |
|
87 | { |
||
88 | 38 | return $this->formatter; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Sets the formatter. |
||
93 | * |
||
94 | * @param \Ivory\HttpAdapter\Event\Formatter\FormatterInterface $formatter The formatter. |
||
95 | */ |
||
96 | 285 | public function setFormatter(FormatterInterface $formatter) |
|
97 | { |
||
98 | 285 | $this->formatter = $formatter; |
|
99 | 285 | } |
|
100 | |||
101 | /** |
||
102 | * Gets the lifetime. |
||
103 | * |
||
104 | * @return integer|null The life time. |
||
105 | */ |
||
106 | 38 | public function getlifetime() |
|
107 | { |
||
108 | 38 | return $this->lifetime; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Sets the lifetime. |
||
113 | * |
||
114 | * @param integer|null $lifetime The life time. |
||
115 | */ |
||
116 | 285 | public function setlifetime($lifetime = null) |
|
117 | { |
||
118 | 285 | $this->lifetime = $lifetime; |
|
119 | 285 | } |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 285 | public function cacheException($cacheException = null) |
|
125 | { |
||
126 | 285 | if ($cacheException !== null) { |
|
127 | 285 | $this->cacheException = $cacheException; |
|
128 | 270 | } |
|
129 | |||
130 | 285 | return $this->cacheException; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 38 | View Code Duplication | public function getResponse(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory) |
0 ignored issues
–
show
|
|||
137 | { |
||
138 | 38 | if (!$this->adapter->has($id = $this->getIdentifier($internalRequest, 'response'))) { |
|
139 | 19 | return; |
|
140 | } |
||
141 | |||
142 | 19 | $response = $this->unserializeResponse($this->adapter->get($id), $messageFactory); |
|
143 | |||
144 | 19 | return $response->withParameter('request', $internalRequest); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 57 | View Code Duplication | public function getException(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
151 | { |
||
152 | 57 | if (!$this->cacheException || !$this->adapter->has($id = $this->getIdentifier($internalRequest, 'exception'))) { |
|
153 | 38 | return; |
|
154 | } |
||
155 | |||
156 | 19 | $exception = $this->unserializeException($this->adapter->get($id)); |
|
157 | 19 | $exception->setRequest($internalRequest); |
|
158 | |||
159 | 19 | return $exception; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | 38 | public function saveResponse(ResponseInterface $response, InternalRequestInterface $internalRequest) |
|
166 | { |
||
167 | 38 | if (!$this->adapter->has($id = $this->getIdentifier($internalRequest, 'response'))) { |
|
168 | 19 | $this->adapter->set($id, $this->serializeResponse($response), $this->lifetime); |
|
169 | 18 | } |
|
170 | 38 | } |
|
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 57 | public function saveException(HttpAdapterException $exception, InternalRequestInterface $internalRequest) |
|
176 | { |
||
177 | 57 | if ($this->cacheException && !$this->adapter->has($id = $this->getIdentifier($internalRequest, 'exception'))) { |
|
178 | 19 | $this->adapter->set($id, $this->serializeException($exception), $this->lifetime); |
|
179 | 18 | } |
|
180 | 57 | } |
|
181 | |||
182 | /** |
||
183 | * Gets the adapter identifier. |
||
184 | * |
||
185 | * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. |
||
186 | * @param string $context The context. |
||
187 | * |
||
188 | * @return string The adapter identifier. |
||
189 | */ |
||
190 | 152 | private function getIdentifier(InternalRequestInterface $internalRequest, $context) |
|
191 | { |
||
192 | 152 | return sha1($context.$this->serializeInternalRequest($internalRequest)); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Serializes an internal request. |
||
197 | * |
||
198 | * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. |
||
199 | * |
||
200 | * @return string The serialized internal request. |
||
201 | */ |
||
202 | 152 | private function serializeInternalRequest(InternalRequestInterface $internalRequest) |
|
203 | { |
||
204 | 152 | $formattedInternalRequest = $this->formatter->formatRequest($internalRequest); |
|
205 | 152 | unset($formattedInternalRequest['parameters']); |
|
206 | |||
207 | 152 | return $this->serialize($formattedInternalRequest); |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Serializes a response. |
||
212 | * |
||
213 | * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. |
||
214 | * |
||
215 | * @return string The serialized response. |
||
216 | */ |
||
217 | 19 | private function serializeResponse(ResponseInterface $response) |
|
218 | { |
||
219 | 19 | return $this->serialize($this->formatter->formatResponse($response)); |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * Serializes an exception. |
||
224 | * |
||
225 | * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception. |
||
226 | * |
||
227 | * @return string The serialized exception. |
||
228 | */ |
||
229 | 19 | private function serializeException(HttpAdapterException $exception) |
|
230 | { |
||
231 | 19 | return $this->serialize($this->formatter->formatException($exception)); |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * Unserializes a response. |
||
236 | * |
||
237 | * @param string $serialized The cached response. |
||
238 | * @param \Ivory\HttpAdapter\Message\MessageFactoryInterface $messageFactory The message factory. |
||
239 | * |
||
240 | * @return \Ivory\HttpAdapter\Message\ResponseInterface The response. |
||
241 | */ |
||
242 | 19 | private function unserializeResponse($serialized, MessageFactoryInterface $messageFactory) |
|
243 | { |
||
244 | 19 | return $this->createResponse($this->unserialize($serialized), $messageFactory); |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * Unserializes an exception. |
||
249 | * |
||
250 | * @param string $serialized The cached exception. |
||
251 | *s |
||
252 | * @return \Ivory\HttpAdapter\HttpAdapterException The exception. |
||
253 | */ |
||
254 | 19 | private function unserializeException($serialized) |
|
255 | { |
||
256 | 19 | return $this->createException($this->unserialize($serialized)); |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * Creates a response. |
||
261 | * |
||
262 | * @param array $unserialized The unserialized response. |
||
263 | * @param \Ivory\HttpAdapter\Message\MessageFactoryInterface $messageFactory The message factory. |
||
264 | * |
||
265 | * @return \Ivory\HttpAdapter\Message\ResponseInterface The response. |
||
266 | */ |
||
267 | 19 | private function createResponse(array $unserialized, MessageFactoryInterface $messageFactory) |
|
268 | { |
||
269 | 19 | return $messageFactory->createResponse( |
|
270 | 19 | $unserialized['status_code'], |
|
271 | 19 | $unserialized['protocol_version'], |
|
272 | 19 | $unserialized['headers'], |
|
273 | 19 | $unserialized['body'], |
|
274 | 19 | $unserialized['parameters'] |
|
275 | 18 | ); |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * Creates an exception. |
||
280 | * |
||
281 | * @param array $unserialized The unserialized exception. |
||
282 | * |
||
283 | * @return \Ivory\HttpAdapter\HttpAdapterException The exception. |
||
284 | */ |
||
285 | 19 | private function createException(array $unserialized) |
|
286 | { |
||
287 | 19 | return new HttpAdapterException($unserialized['message']); |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * Serializes data. |
||
292 | * |
||
293 | * @param array $data The data. |
||
294 | * |
||
295 | * @return string The serialized data. |
||
296 | */ |
||
297 | 152 | private function serialize(array $data) |
|
298 | { |
||
299 | 152 | return json_encode($data); |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * Unserializes data. |
||
304 | * |
||
305 | * @param string $data The serialized data. |
||
306 | * |
||
307 | * @return array The unserialized data. |
||
308 | */ |
||
309 | 38 | private function unserialize($data) |
|
310 | { |
||
311 | 38 | return json_decode($data, true); |
|
312 | } |
||
313 | } |
||
314 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.