Cache::cacheException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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\Cache\Adapter\CacheAdapterInterface;
15
use Ivory\HttpAdapter\Event\Formatter\Formatter;
16
use Ivory\HttpAdapter\Event\Formatter\FormatterInterface;
17
use Ivory\HttpAdapter\HttpAdapterException;
18
use Ivory\HttpAdapter\Message\InternalRequestInterface;
19
use Ivory\HttpAdapter\Message\MessageFactoryInterface;
20
use Ivory\HttpAdapter\Message\ResponseInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class Cache implements CacheInterface
26
{
27
    /**
28
     * @var CacheAdapterInterface
29
     */
30
    private $adapter;
31
32
    /**
33
     * @var FormatterInterface
34
     */
35
    private $formatter;
36
37
    /**
38
     * @var int|null
39
     */
40
    private $lifetime;
41
42
    /**
43
     * @var bool
44
     */
45
    private $cacheException;
46
47
    /**
48
     * @param CacheAdapterInterface   $adapter
49
     * @param FormatterInterface|null $formatter
50
     * @param int|null                $lifetime
51
     * @param bool                    $cacheException
52
     */
53 135
    public function __construct(
54
        CacheAdapterInterface $adapter,
55
        FormatterInterface $formatter = null,
56
        $lifetime = null,
57
        $cacheException = true
58
    ) {
59 135
        $this->setAdapter($adapter);
60 135
        $this->setFormatter($formatter ?: new Formatter());
61 135
        $this->setlifetime($lifetime);
62 135
        $this->cacheException($cacheException);
63 135
    }
64
65
    /**
66
     * @return CacheAdapterInterface
67
     */
68 18
    public function getAdapter()
69
    {
70 18
        return $this->adapter;
71
    }
72
73
    /**
74
     * @param CacheAdapterInterface $adapter
75
     */
76 135
    public function setAdapter(CacheAdapterInterface $adapter)
77
    {
78 135
        $this->adapter = $adapter;
79 135
    }
80
81
    /**
82
     * @return FormatterInterface
83
     */
84 18
    public function getFormatter()
85
    {
86 18
        return $this->formatter;
87
    }
88
89
    /**
90
     * @param FormatterInterface $formatter
91
     */
92 135
    public function setFormatter(FormatterInterface $formatter)
93
    {
94 135
        $this->formatter = $formatter;
95 135
    }
96
97
    /**
98
     * @return int|null
99
     */
100 18
    public function getLifetime()
101
    {
102 18
        return $this->lifetime;
103
    }
104
105
    /**
106
     * @param int|null $lifetime
107
     */
108 135
    public function setLifetime($lifetime = null)
109
    {
110 135
        $this->lifetime = $lifetime;
111 135
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 135
    public function cacheException($cacheException = null)
117
    {
118 135
        if ($cacheException !== null) {
119 135
            $this->cacheException = $cacheException;
120 105
        }
121
122 135
        return $this->cacheException;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 18 View Code Duplication
    public function getResponse(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory)
0 ignored issues
show
Duplication introduced by
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...
129
    {
130 18
        if (!$this->adapter->has($id = $this->getIdentifier($internalRequest, 'response'))) {
131 9
            return;
132
        }
133
134 9
        $response = $this->unserializeResponse($this->adapter->get($id), $messageFactory);
135
136 9
        return $response->withParameter('request', $internalRequest);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 27 View Code Duplication
    public function getException(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory)
0 ignored issues
show
Duplication introduced by
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...
143
    {
144 27
        if (!$this->cacheException || !$this->adapter->has($id = $this->getIdentifier($internalRequest, 'exception'))) {
145 18
            return;
146
        }
147
148 9
        $exception = $this->unserializeException($this->adapter->get($id));
149 9
        $exception->setRequest($internalRequest);
150
151 9
        return $exception;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 18
    public function saveResponse(ResponseInterface $response, InternalRequestInterface $internalRequest)
158
    {
159 18
        if (!$this->adapter->has($id = $this->getIdentifier($internalRequest, 'response'))) {
160 9
            $this->adapter->set($id, $this->serializeResponse($response), $this->lifetime);
161 7
        }
162 18
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 27
    public function saveException(HttpAdapterException $exception, InternalRequestInterface $internalRequest)
168
    {
169 27
        if ($this->cacheException && !$this->adapter->has($id = $this->getIdentifier($internalRequest, 'exception'))) {
170 9
            $this->adapter->set($id, $this->serializeException($exception), $this->lifetime);
171 7
        }
172 27
    }
173
174
    /**
175
     * @param InternalRequestInterface $internalRequest
176
     * @param string                   $context
177
     *
178
     * @return string
179
     */
180 72
    private function getIdentifier(InternalRequestInterface $internalRequest, $context)
181
    {
182 72
        return sha1($context.$this->serializeInternalRequest($internalRequest));
183
    }
184
185
    /**
186
     * @param InternalRequestInterface $internalRequest
187
     *
188
     * @return string
189
     */
190 72
    private function serializeInternalRequest(InternalRequestInterface $internalRequest)
191
    {
192 72
        $formattedInternalRequest = $this->formatter->formatRequest($internalRequest);
193 72
        unset($formattedInternalRequest['parameters']);
194
195 72
        return $this->serialize($formattedInternalRequest);
196
    }
197
198
    /**
199
     * @param ResponseInterface $response
200
     *
201
     * @return string
202
     */
203 9
    private function serializeResponse(ResponseInterface $response)
204
    {
205 9
        return $this->serialize($this->formatter->formatResponse($response));
206
    }
207
208
    /**
209
     * @param HttpAdapterException $exception
210
     *
211
     * @return string
212
     */
213 9
    private function serializeException(HttpAdapterException $exception)
214
    {
215 9
        return $this->serialize($this->formatter->formatException($exception));
216
    }
217
218
    /**
219
     * @param string                  $serialized
220
     * @param MessageFactoryInterface $messageFactory
221
     *
222
     * @return ResponseInterface
223
     */
224 9
    private function unserializeResponse($serialized, MessageFactoryInterface $messageFactory)
225
    {
226 9
        return $this->createResponse($this->unserialize($serialized), $messageFactory);
227
    }
228
229
    /**
230
     * @param string $serialized
231
     *
232
     * @return HttpAdapterException
233
     */
234 9
    private function unserializeException($serialized)
235
    {
236 9
        return $this->createException($this->unserialize($serialized));
237
    }
238
239
    /**
240
     * @param array                   $unserialized
241
     * @param MessageFactoryInterface $messageFactory
242
     *
243
     * @return ResponseInterface
244
     */
245 9
    private function createResponse(array $unserialized, MessageFactoryInterface $messageFactory)
246
    {
247 9
        return $messageFactory->createResponse(
248 9
            $unserialized['status_code'],
249 9
            $unserialized['protocol_version'],
250 9
            $unserialized['headers'],
251 9
            $unserialized['body'],
252 9
            $unserialized['parameters']
253 7
        );
254
    }
255
256
    /**
257
     * @param array $unserialized
258
     *
259
     * @return HttpAdapterException
260
     */
261 9
    private function createException(array $unserialized)
262
    {
263 9
        return new HttpAdapterException($unserialized['message']);
264
    }
265
266
    /**
267
     * @param array $data
268
     *
269
     * @return string
270
     */
271 72
    private function serialize(array $data)
272
    {
273 72
        return json_encode($data);
274
    }
275
276
    /**
277
     * @param string $data
278
     *
279
     * @return array
280
     */
281 18
    private function unserialize($data)
282
    {
283 18
        return json_decode($data, true);
284
    }
285
}
286