CacheKeyBuilder::setDoctrine()   A
last analyzed

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 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
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
10
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service;
11
12
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\CacheKeyBuilder\EtagHasherInterface;
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class CacheKeyBuilder
17
{
18
    /**
19
     * @var string
20
     */
21
    const IDENTIFIER_SEPARATOR = '|';
22
23
    /**
24
     * @var string
25
     */
26
    const IDENTIFIER_PREFIX = ':';
27
28
    /**
29
     * @var EtagHasherInterface
30
     */
31
    protected $etag_hasher;
32
33
    /**
34
     * @var Registry|null
35
     */
36
    protected $doctrine;
37
38
    /**
39
     * @param EtagHasherInterface $etag_hasher
40
     */
41 9
    public function __construct(EtagHasherInterface $etag_hasher)
42
    {
43 9
        $this->etag_hasher = $etag_hasher;
44 9
    }
45
46
    /**
47
     * @param Registry $doctrine
48
     *
49
     * @return CacheKeyBuilder
50
     */
51 9
    public function setDoctrine(Registry $doctrine)
52
    {
53 9
        $this->doctrine = $doctrine;
54
55 9
        return $this;
56
    }
57
58
    /**
59
     * @param object $entity
60
     *
61
     * @return string|null
62
     */
63 4
    public function getEntityAlias($entity)
64
    {
65 4
        if (!($this->doctrine instanceof Registry)) {
66 1
            return null;
67
        }
68
69 3
        $class = get_class($entity);
70
71 3
        $namespaces = $this
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConfiguration() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
72
            ->doctrine
73 3
            ->getManager()
74 3
            ->getConfiguration()
75 3
            ->getEntityNamespaces();
76
77 3
        foreach ($namespaces as $ns_alias => $ns) {
78 3
            if (strpos($class, $ns) === 0) {
79 2
                return $ns_alias.':'.ltrim(str_replace($ns, '', $class), '\\');
80
            }
81 3
        }
82
83 1
        return null;
84
    }
85
86
    /**
87
     * @param object $entity
88
     *
89
     * @return string|null
90
     */
91 4
    public function getEntityIdentifier($entity)
92
    {
93 4
        if (!($this->doctrine instanceof Registry)) {
94 1
            return null;
95
        }
96
97 3
        $ids = $this
98
            ->doctrine
99 3
            ->getManager()
100 3
            ->getClassMetadata(get_class($entity))
101 3
            ->getIdentifierValues($entity);
102
103 3
        return $ids ? self::IDENTIFIER_PREFIX.implode(self::IDENTIFIER_SEPARATOR, $ids) : null;
104
    }
105
106
    /**
107
     * @param Response $response
108
     *
109
     * @return string
110
     */
111 1
    public function getEtag(Response $response)
112
    {
113 1
        return $this->etag_hasher->hash($response);
114
    }
115
}
116