These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @author Bram Gerritsen [email protected] |
||
4 | * @copyright (c) Bram Gerritsen 2013 |
||
5 | * @license http://opensource.org/licenses/mit-license.php |
||
6 | */ |
||
7 | |||
8 | namespace StrokerCache\Listener; |
||
9 | |||
10 | use StrokerCache\Options\ModuleOptions; |
||
11 | use StrokerCache\Service\CacheService; |
||
12 | use Zend\EventManager\AbstractListenerAggregate; |
||
13 | use Zend\EventManager\EventManagerInterface; |
||
14 | use Zend\Http\PhpEnvironment\Request as HttpRequest; |
||
15 | use Zend\Mvc\MvcEvent; |
||
16 | |||
17 | class CacheListener extends AbstractListenerAggregate |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $listeners = array(); |
||
23 | |||
24 | /** |
||
25 | * @var CacheService |
||
26 | */ |
||
27 | protected $cacheService; |
||
28 | |||
29 | /** |
||
30 | * @var ModuleOptions |
||
31 | */ |
||
32 | protected $options; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $loadedFromCache = false; |
||
38 | |||
39 | /** |
||
40 | * Default constructor |
||
41 | * |
||
42 | * @param CacheService $cacheService |
||
43 | * @param ModuleOptions $options |
||
44 | */ |
||
45 | 8 | public function __construct(CacheService $cacheService, ModuleOptions $options) |
|
46 | { |
||
47 | 8 | $this->cacheService = $cacheService; |
|
48 | 8 | $this->options = $options; |
|
49 | 8 | } |
|
50 | |||
51 | /** |
||
52 | * {@inheritDoc} |
||
53 | */ |
||
54 | 1 | public function attach(EventManagerInterface $events, $priority = 1) |
|
55 | { |
||
56 | 1 | $this->listeners[] = $events->attach('route', [$this, 'onRoute'], 100); |
|
57 | 1 | $this->listeners[] = $events->attach('finish', [$this, 'onFinish'], -100); |
|
58 | 1 | } |
|
59 | |||
60 | /** |
||
61 | * Load the page contents from the cache and set the response. |
||
62 | * |
||
63 | * @param MvcEvent $e |
||
64 | * @return \Zend\Stdlib\ResponseInterface|void |
||
65 | */ |
||
66 | 6 | public function onRoute(MvcEvent $e) |
|
67 | { |
||
68 | 6 | if (!$e->getRequest() instanceof HttpRequest) { |
|
69 | 1 | return; |
|
70 | } |
||
71 | |||
72 | 5 | $data = $this->getCacheService()->load($e); |
|
73 | |||
74 | 5 | if ($data !== null) { |
|
75 | 3 | $this->loadedFromCache = true; |
|
76 | |||
77 | 3 | if ($this->getOptions()->getCacheResponse() === true) { |
|
78 | 2 | $response = unserialize($data); |
|
79 | 2 | } else { |
|
80 | 1 | $response = $e->getResponse(); |
|
81 | 1 | $response->setContent($data); |
|
82 | } |
||
83 | |||
84 | 3 | $response->getHeaders()->addHeaderLine('X-Stroker-Cache', 'Hit'); |
|
85 | |||
86 | 3 | return $response; |
|
87 | } |
||
88 | 2 | $e->getResponse()->getHeaders()->addHeaderLine('X-Stroker-Cache', 'Miss'); |
|
0 ignored issues
–
show
|
|||
89 | 2 | } |
|
90 | |||
91 | /** |
||
92 | * Save page contents to the cache |
||
93 | * |
||
94 | * @param MvcEvent $e |
||
95 | * @return void |
||
96 | */ |
||
97 | 4 | public function onFinish(MvcEvent $e) |
|
98 | { |
||
99 | 4 | if (!$e->getRequest() instanceof HttpRequest || $this->loadedFromCache) { |
|
100 | 3 | return; |
|
101 | } |
||
102 | |||
103 | 1 | $this->getCacheService()->save($e); |
|
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * @return CacheService |
||
108 | */ |
||
109 | 5 | public function getCacheService() |
|
110 | { |
||
111 | 5 | return $this->cacheService; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return ModuleOptions |
||
116 | */ |
||
117 | 3 | public function getOptions() |
|
118 | { |
||
119 | 3 | return $this->options; |
|
120 | } |
||
121 | } |
||
122 |
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: