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 |
|
|
|
|
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
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: