Completed
Pull Request — master (#14)
by Pavel
05:32 queued 02:29
created

ApiEntityCache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Cache;
4
5
use Bankiru\Api\Doctrine\EntityDataCacheInterface;
6
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
7
use Psr\Cache\CacheItemPoolInterface;
8
9
class ApiEntityCache implements EntityDataCacheInterface
10
{
11
    /** @var  CacheItemPoolInterface */
12
    private $cache;
13
    /** @var ApiMetadata */
14
    private $metadata;
15
    /** @var CacheConfiguration */
16
    private $configuration;
17
18
    /**
19
     * ApiEntityCache constructor.
20
     *
21
     * @param CacheItemPoolInterface $cache
22
     * @param ApiMetadata            $metadata
23
     * @param CacheConfiguration     $configuration
24
     */
25 1
    public function __construct(
26
        CacheItemPoolInterface $cache,
27
        ApiMetadata $metadata,
28
        CacheConfiguration $configuration
29
    ) {
30 1
        $this->cache         = $cache;
31 1
        $this->metadata      = $metadata;
32 1
        $this->configuration = $configuration;
33 1
    }
34
35
    /** {@inheritdoc} */
36 1
    public function get(array $identifier)
37
    {
38 1
        if (!$this->configuration->isEnabled()) {
39
            return null;
40
        }
41
42 1
        return $this->cache->getItem($this->getKey($identifier))->get();
43
    }
44
45
    /** {@inheritdoc} */
46 1
    public function set(array $identifier, $data)
47
    {
48 1
        if (!$this->configuration->isEnabled()) {
49
            return;
50
        }
51
52 1
        $this->cache->save(
53 1
            $this->cache
54 1
                ->getItem($this->getKey($identifier))
55 1
                ->set($data)
56 1
                ->expiresAfter($this->configuration->getTtl())
57 1
        );
58 1
    }
59
60
    /** {@inheritdoc} */
61 1
    public function getMetadata()
62
    {
63 1
        return $this->metadata;
64
    }
65
66
    /**
67
     * @param array $identifier
68
     *
69
     * @return string
70
     */
71 1
    private function getKey(array $identifier)
72
    {
73 1
        return $this->configuration->getStrategy()->getEntityKey($this->metadata, $identifier);
74
    }
75
}
76