1 | <?php |
||
36 | class GenericCacheAdapter implements CacheAdapterInterface |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * The serial of the actual import process. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $serial; |
||
45 | |||
46 | /** |
||
47 | * The cache for the query results. |
||
48 | * |
||
49 | * @var \Psr\Cache\CacheItemPoolInterface |
||
50 | */ |
||
51 | protected $cache; |
||
52 | |||
53 | /** |
||
54 | * References that links to another cache entry. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $references = array(); |
||
59 | |||
60 | /** |
||
61 | * The cache key utility instance. |
||
62 | * |
||
63 | * @var \TechDivision\Import\Utils\CacheKeyUtilInterface |
||
64 | */ |
||
65 | protected $cacheKeyUtil; |
||
66 | |||
67 | /** |
||
68 | * Initialize the cache handler with the passed cache and configuration instances. |
||
69 | * . |
||
70 | * @param \Psr\Cache\CacheItemPoolInterface $cache The cache instance |
||
71 | * @param \TechDivision\Import\Utils\CacheKeyUtilInterface $cacheKeyUtil The cache key utility instance |
||
72 | * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
||
73 | */ |
||
74 | public function __construct( |
||
87 | |||
88 | /** |
||
89 | * Resolve's the cache key. |
||
90 | * |
||
91 | * @param string $from The cache key to resolve |
||
92 | * |
||
93 | * @return string The resolved reference |
||
94 | */ |
||
95 | protected function resolveReference($from) |
||
106 | |||
107 | /** |
||
108 | * Creates a unique cache key from the passed data. |
||
109 | * |
||
110 | * @param mixed $data The date to create the cache key from |
||
111 | * |
||
112 | * @return string The generated cache key |
||
113 | */ |
||
114 | public function cacheKey($data) |
||
118 | |||
119 | /** |
||
120 | * Query whether or not a cache value for the passed cache key is available. |
||
121 | * |
||
122 | * @param string $key The cache key to query for |
||
123 | * |
||
124 | * @return boolean TRUE if the a value is available, else FALSE |
||
125 | */ |
||
126 | public function isCached($key) |
||
137 | |||
138 | /** |
||
139 | * Inversion of the isCached() method. |
||
140 | * |
||
141 | * @param string $key The cache key to query for |
||
142 | * |
||
143 | * @return boolean TRUE if the value is not available, else FALSE |
||
144 | */ |
||
145 | public function notCached($key) |
||
149 | |||
150 | /** |
||
151 | * Add's a cache reference from one key to another. |
||
152 | * |
||
153 | * @param string $from The key to reference from |
||
154 | * @param string $to The key to reference to |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | public function addReference($from, $to) |
||
162 | |||
163 | /** |
||
164 | * Add the passed item to the cache. |
||
165 | * |
||
166 | * @param string $key The cache key to use |
||
167 | * @param mixed $value The value that has to be cached |
||
168 | * @param array $references An array with references to add |
||
169 | * @param boolean $override Flag that allows to override an exising cache entry |
||
170 | * @param integer $time The TTL in seconds for the passed item |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | public function toCache($key, $value, array $references = array(), $override = false, $time = null) |
||
197 | |||
198 | /** |
||
199 | * Returns a new cache item for the passed key |
||
200 | * |
||
201 | * @param string $key The cache key to return the item for |
||
202 | * |
||
203 | * @return mixed The value for the passed key |
||
204 | */ |
||
205 | public function fromCache($key) |
||
209 | |||
210 | /** |
||
211 | * Flush the cache and remove the references. |
||
212 | * |
||
213 | * @return void |
||
214 | */ |
||
215 | public function flushCache() |
||
220 | |||
221 | /** |
||
222 | * Remove the item with the passed key and all its references from the cache. |
||
223 | * |
||
224 | * @param string $key The key of the cache item to Remove |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | public function removeCache($key) |
||
233 | |||
234 | /** |
||
235 | * Raises the value for the attribute with the passed key by one. |
||
236 | * |
||
237 | * @param mixed $key The key of the attribute to raise the value for |
||
238 | * @param mixed $counterName The name of the counter to raise |
||
239 | * |
||
240 | * @return integer The counter's new value |
||
241 | */ |
||
242 | public function raiseCounter($key, $counterName) |
||
260 | |||
261 | /** |
||
262 | * This method merges the passed attributes with an array that |
||
263 | * has already been added under the passed key. |
||
264 | * |
||
265 | * If no value will be found under the passed key, the attributes |
||
266 | * will simply be registered. |
||
267 | * |
||
268 | * @param mixed $key The key of the attributes that has to be merged with the passed ones |
||
269 | * @param array $attributes The attributes that has to be merged with the exising ones |
||
270 | * |
||
271 | * @return void |
||
272 | * @throws \Exception Is thrown, if the already registered value is no array |
||
273 | * @link http://php.net/array_replace_recursive |
||
274 | */ |
||
275 | public function mergeAttributesRecursive($key, array $attributes) |
||
293 | } |
||
294 |
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: