Completed
Push — master ( 99d075...0c659c )
by Pavel
26:03
created

ApiEntityCache::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 CacheConfigurationInterface */
16
    private $configuration;
17
18
    /**
19
     * ApiEntityCache constructor.
20
     *
21
     * @param CacheItemPoolInterface      $cache
22
     * @param ApiMetadata                 $metadata
23
     * @param CacheConfigurationInterface $configuration
24
     */
25 1
    public function __construct(
26
        CacheItemPoolInterface $cache,
27
        ApiMetadata $metadata,
28
        CacheConfigurationInterface $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
    /** {@inheritdoc} */
67
    public function getConfiguration()
68
    {
69
        return $this->configuration;
70
    }
71
72
73
    /**
74
     * @param array $identifier
75
     *
76
     * @return string
77
     */
78 1
    private function getKey(array $identifier)
79
    {
80 1
        return $this->configuration->getStrategy()->getEntityKey($this->metadata, $identifier);
81
    }
82
83
    /**
84
     * Clears the cache for given entity identifier
85
     *
86
     * @param array $identifier
87
     *
88
     * @return void
89
     */
90
    public function clear(array $identifier)
91
    {
92
        $this->cache->deleteItem($this->getKey($identifier));
93
    }
94
}
95