Completed
Pull Request — master (#14)
by Pavel
29:07 queued 23:49
created

LoggingCache::get()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Cache;
4
5
use Bankiru\Api\Doctrine\EntityDataCacheInterface;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
9
final class LoggingCache implements EntityDataCacheInterface
10
{
11
    /** @var  EntityDataCacheInterface */
12
    private $delegate;
13
    /** @var  LoggerInterface */
14
    private $logger;
15
16
    /**
17
     * LoggingCache constructor.
18
     *
19
     * @param EntityDataCacheInterface $delegate
20
     * @param LoggerInterface          $logger
21
     */
22
    public function __construct(EntityDataCacheInterface $delegate, LoggerInterface $logger = null)
23
    {
24
        $this->delegate = $delegate;
25
        $this->logger   = $logger ?: new NullLogger();
26
    }
27
28
    /** {@inheritdoc} */
29
    public function get(array $identifier)
30
    {
31
        $data = $this->delegate->get($identifier);
32
33
        $this->logger->debug(
34
            sprintf('Entity cache %s', null === $data ? 'HIT' : 'MISS'),
35
            ['class' => $this->getMetadata()->getName(), 'identifiers' => $identifier]
36
        );
37
38
        return $data;
39
    }
40
41
    /** {@inheritdoc} */
42
    public function set(array $identifier, $data)
43
    {
44
        $this->delegate->set($identifier, $data);
45
46
        $this->logger->debug(
47
            'Stored API entity data to cache',
48
            ['class' => $this->getMetadata()->getName(), 'identifiers' => $identifier]
49
        );
50
    }
51
52
    /** {@inheritdoc} */
53
    public function getMetadata()
54
    {
55
        return $this->delegate->getMetadata();
56
    }
57
}
58