These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Ivory Http Adapter package. |
||
5 | * |
||
6 | * (c) Eric GELOEN <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please read the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Ivory\HttpAdapter\Event\Subscriber; |
||
13 | |||
14 | use Ivory\HttpAdapter\Event\Cache\CacheInterface; |
||
15 | use Ivory\HttpAdapter\Event\Events; |
||
16 | use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent; |
||
17 | use Ivory\HttpAdapter\Event\MultiRequestErroredEvent; |
||
18 | use Ivory\HttpAdapter\Event\MultiRequestSentEvent; |
||
19 | use Ivory\HttpAdapter\Event\RequestCreatedEvent; |
||
20 | use Ivory\HttpAdapter\Event\RequestErroredEvent; |
||
21 | use Ivory\HttpAdapter\Event\RequestSentEvent; |
||
22 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
23 | |||
24 | /** |
||
25 | * Cache subscriber. |
||
26 | * |
||
27 | * @author GeLo <[email protected]> |
||
28 | */ |
||
29 | class CacheSubscriber implements EventSubscriberInterface |
||
30 | { |
||
31 | /** @var \Ivory\HttpAdapter\Event\Cache\CacheInterface */ |
||
32 | private $cache; |
||
33 | |||
34 | /** |
||
35 | * Creates a cache subscriber. |
||
36 | * |
||
37 | * @param \Ivory\HttpAdapter\Event\Cache\CacheInterface $cache The cache |
||
38 | */ |
||
39 | 228 | public function __construct(CacheInterface $cache) |
|
40 | { |
||
41 | 228 | $this->setCache($cache); |
|
42 | 228 | } |
|
43 | |||
44 | /** |
||
45 | * Gets the cache. |
||
46 | * |
||
47 | * @return \Ivory\HttpAdapter\Event\Cache\CacheInterface The cache. |
||
48 | */ |
||
49 | 19 | public function getCache() |
|
50 | { |
||
51 | 19 | return $this->cache; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Sets the cache. |
||
56 | * |
||
57 | * @param \Ivory\HttpAdapter\Event\Cache\CacheInterface $cache The cache. |
||
58 | */ |
||
59 | 228 | public function setCache(CacheInterface $cache) |
|
60 | { |
||
61 | 228 | $this->cache = $cache; |
|
62 | 228 | } |
|
63 | |||
64 | /** |
||
65 | * On request created. |
||
66 | * |
||
67 | * @param \Ivory\HttpAdapter\Event\RequestCreatedEvent $event |
||
68 | */ |
||
69 | 57 | public function onRequestCreated(RequestCreatedEvent $event) |
|
70 | { |
||
71 | 57 | $request = $event->getRequest(); |
|
72 | 57 | $messageFactory = $event->getHttpAdapter()->getConfiguration()->getMessageFactory(); |
|
73 | |||
74 | 57 | if (($response = $this->cache->getResponse($request, $messageFactory)) !== null) { |
|
75 | 19 | $event->setResponse($response); |
|
76 | 56 | } elseif (($exception = $this->cache->getException($request, $messageFactory)) !== null) { |
|
77 | 19 | $event->setException($exception); |
|
78 | 18 | } |
|
79 | 57 | } |
|
80 | |||
81 | /** |
||
82 | * On request sent. |
||
83 | * |
||
84 | * @param \Ivory\HttpAdapter\Event\RequestSentEvent $event |
||
85 | */ |
||
86 | 19 | public function onRequestSent(RequestSentEvent $event) |
|
87 | { |
||
88 | 19 | $this->cache->saveResponse($event->getResponse(), $event->getRequest()); |
|
89 | 19 | } |
|
90 | |||
91 | /** |
||
92 | * On request errored. |
||
93 | * |
||
94 | * @param \Ivory\HttpAdapter\Event\RequestErroredEvent $event |
||
95 | */ |
||
96 | 19 | public function onRequestErrored(RequestErroredEvent $event) |
|
97 | { |
||
98 | 19 | $this->cache->saveException($event->getException(), $event->getException()->getRequest()); |
|
99 | 19 | } |
|
100 | |||
101 | /** |
||
102 | * On multi request created. |
||
103 | * |
||
104 | * @param \Ivory\HttpAdapter\Event\MultiRequestCreatedEvent $event |
||
105 | */ |
||
106 | 57 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
107 | { |
||
108 | 57 | $messageFactory = $event->getHttpAdapter()->getConfiguration()->getMessageFactory(); |
|
109 | |||
110 | 57 | foreach ($event->getRequests() as $request) { |
|
111 | 57 | if (($response = $this->cache->getResponse($request, $messageFactory)) !== null) { |
|
112 | 19 | $event->addResponse($response); |
|
113 | 19 | $event->removeRequest($request); |
|
114 | 56 | } elseif (($exception = $this->cache->getException($request, $messageFactory)) !== null) { |
|
115 | 19 | $event->addException($exception); |
|
116 | 21 | $event->removeRequest($request); |
|
117 | 18 | } |
|
118 | 54 | } |
|
119 | 57 | } |
|
120 | |||
121 | /** |
||
122 | * On multi request sent. |
||
123 | * |
||
124 | * @param \Ivory\HttpAdapter\Event\MultiRequestSentEvent $event |
||
125 | */ |
||
126 | 19 | public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
127 | { |
||
128 | 19 | foreach ($event->getResponses() as $response) { |
|
129 | 19 | $this->cache->saveResponse($response, $response->getParameter('request')); |
|
130 | 18 | } |
|
131 | 19 | } |
|
132 | |||
133 | /** |
||
134 | * On multi request errored. |
||
135 | * |
||
136 | * @param \Ivory\HttpAdapter\Event\MultiRequestErroredEvent $event |
||
137 | */ |
||
138 | 19 | public function onMultiRequestErrored(MultiRequestErroredEvent $event) |
|
139 | { |
||
140 | 19 | foreach ($event->getResponses() as $response) { |
|
141 | 19 | $this->cache->saveResponse($response, $response->getParameter('request')); |
|
142 | 18 | } |
|
143 | |||
144 | 19 | foreach ($event->getExceptions() as $exception) { |
|
145 | 19 | $this->cache->saveException($exception, $exception->getRequest()); |
|
146 | 18 | } |
|
147 | 19 | } |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 19 | View Code Duplication | public static function getSubscribedEvents() |
0 ignored issues
–
show
|
|||
153 | { |
||
154 | return [ |
||
155 | 19 | Events::REQUEST_CREATED => ['onRequestCreated', -100], |
|
156 | 18 | Events::REQUEST_SENT => ['onRequestSent', -100], |
|
157 | 18 | Events::REQUEST_ERRORED => ['onRequestErrored', -100], |
|
158 | 18 | Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', -100], |
|
159 | 18 | Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', -100], |
|
160 | 18 | Events::MULTI_REQUEST_ERRORED => ['onMultiRequestErrored', -100], |
|
161 | 18 | ]; |
|
162 | } |
||
163 | } |
||
164 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.