Completed
Push — master ( 3c2288...3499eb )
by Peter
04:05
created

CacheKeyBuilder::setEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service;
10
11
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\CacheKeyBuilder\EtagHasherInterface;
12
use Symfony\Bridge\Doctrine\RegistryInterface;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class CacheKeyBuilder
16
{
17
    /**
18
     * @var string
19
     */
20
    const IDENTIFIER_SEPARATOR = '|';
21
22
    /**
23
     * @var string
24
     */
25
    const IDENTIFIER_PREFIX = ':';
26
27
    /**
28
     * @var EtagHasherInterface
29
     */
30
    protected $etag_hasher;
31
32
    /**
33
     * @var RegistryInterface|null
34
     */
35
    protected $doctrine;
36
37
    /**
38
     * @param EtagHasherInterface $etag_hasher
39
     */
40 9
    public function __construct(EtagHasherInterface $etag_hasher)
41
    {
42 9
        $this->etag_hasher = $etag_hasher;
43 9
    }
44
45
    /**
46
     * @param RegistryInterface $doctrine
47
     *
48
     * @return CacheKeyBuilder
49
     */
50 9
    public function setDoctrine(RegistryInterface $doctrine)
51
    {
52 9
        $this->doctrine = $doctrine;
53
54 9
        return $this;
55
    }
56
57
    /**
58
     * @param object $entity
59
     *
60
     * @return string|null
61
     */
62 4
    public function getEntityAlias($entity)
63
    {
64 4
        if (!($this->doctrine instanceof RegistryInterface)) {
65 1
            return null;
66
        }
67
68 3
        $class = get_class($entity);
69
70 3
        $namespaces = $this
71
            ->doctrine
72 3
            ->getEntityManager()
73 3
            ->getConfiguration()
74 3
            ->getEntityNamespaces();
75
76 3
        foreach ($namespaces as $ns_alias => $ns) {
77 3
            if (strpos($class, $ns) === 0) {
78 2
                return $ns_alias.':'.ltrim(str_replace($ns, '', $class), '\\');
79
            }
80 3
        }
81
82 1
        return null;
83
    }
84
85
    /**
86
     * @param object $entity
87
     *
88
     * @return string|null
89
     */
90 4
    public function getEntityIdentifier($entity)
91
    {
92 4
        if (!($this->doctrine instanceof RegistryInterface)) {
93 1
            return null;
94
        }
95
96 3
        $ids = $this
97
            ->doctrine
98 3
            ->getEntityManager()
99 3
            ->getClassMetadata(get_class($entity))
100 3
            ->getIdentifierValues($entity);
101
102 3
        return $ids ? self::IDENTIFIER_PREFIX.implode(self::IDENTIFIER_SEPARATOR, $ids) : null;
103
    }
104
105
    /**
106
     * @param Response $response
107
     *
108
     * @return string
109
     */
110 1
    public function getEtag(Response $response)
111
    {
112 1
        return $this->etag_hasher->hash($response);
113
    }
114
}
115