Passed
Push — master ( 654348...8ec1ee )
by Dāvis
03:00
created

PsrAdapter::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Guzzle\Cache;
4
5
use Sludio\HelperBundle\Guzzle\Cache\NamingStrategy\HashNamingStrategy;
6
use GuzzleHttp\Psr7\Response;
7
use Psr\Cache\CacheItemPoolInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class PsrAdapter implements StorageAdapterInterface
12
{
13
    private $cache;
14
    private $namingStrategy;
15
    private $ttl;
16
17
    public function __construct(CacheItemPoolInterface $cache, $ttl = 0)
18
    {
19
        $this->cache = $cache;
20
        $this->namingStrategy = new HashNamingStrategy();
21
        $this->ttl = $ttl;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 View Code Duplication
    public function fetch(RequestInterface $request)
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...
28
    {
29
        $key = $this->namingStrategy->filename($request);
30
31
        $item = $this->cache->getItem($key);
32
33
        if ($item->isHit()) {
34
            $data = $item->get();
35
36
            return new Response($data['status'], $data['headers'], $data['body'], $data['version'], $data['reason']);
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function save(RequestInterface $request, ResponseInterface $response)
44
    {
45
        $key = $this->namingStrategy->filename($request);
46
47
        $item = $this->cache->getItem($key);
48
        $item->expiresAfter($this->ttl);
49
        $item->set([
50
            'status' => $response->getStatusCode(),
51
            'headers' => $response->getHeaders(),
52
            'body' => (string)$response->getBody(),
53
            'version' => $response->getProtocolVersion(),
54
            'reason' => $response->getReasonPhrase(),
55
        ]);
56
57
        $this->cache->save($item);
58
59
        $response->getBody()->seek(0);
60
    }
61
}