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
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class CacheSubscriber implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var CacheInterface |
31
|
|
|
*/ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param CacheInterface $cache |
36
|
|
|
*/ |
37
|
108 |
|
public function __construct(CacheInterface $cache) |
38
|
|
|
{ |
39
|
108 |
|
$this->setCache($cache); |
40
|
108 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return CacheInterface |
44
|
|
|
*/ |
45
|
9 |
|
public function getCache() |
46
|
|
|
{ |
47
|
9 |
|
return $this->cache; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param CacheInterface $cache |
52
|
|
|
*/ |
53
|
108 |
|
public function setCache(CacheInterface $cache) |
54
|
|
|
{ |
55
|
108 |
|
$this->cache = $cache; |
56
|
108 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param RequestCreatedEvent $event |
60
|
|
|
*/ |
61
|
27 |
|
public function onRequestCreated(RequestCreatedEvent $event) |
62
|
|
|
{ |
63
|
27 |
|
$request = $event->getRequest(); |
64
|
27 |
|
$messageFactory = $event->getHttpAdapter()->getConfiguration()->getMessageFactory(); |
65
|
|
|
|
66
|
27 |
|
if (($response = $this->cache->getResponse($request, $messageFactory)) !== null) { |
67
|
9 |
|
$event->setResponse($response); |
68
|
25 |
|
} elseif (($exception = $this->cache->getException($request, $messageFactory)) !== null) { |
69
|
9 |
|
$event->setException($exception); |
70
|
7 |
|
} |
71
|
27 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param RequestSentEvent $event |
75
|
|
|
*/ |
76
|
9 |
|
public function onRequestSent(RequestSentEvent $event) |
77
|
|
|
{ |
78
|
9 |
|
$this->cache->saveResponse($event->getResponse(), $event->getRequest()); |
79
|
9 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param RequestErroredEvent $event |
83
|
|
|
*/ |
84
|
9 |
|
public function onRequestErrored(RequestErroredEvent $event) |
85
|
|
|
{ |
86
|
9 |
|
$this->cache->saveException($event->getException(), $event->getException()->getRequest()); |
87
|
9 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param MultiRequestCreatedEvent $event |
91
|
|
|
*/ |
92
|
27 |
|
public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
93
|
|
|
{ |
94
|
27 |
|
$messageFactory = $event->getHttpAdapter()->getConfiguration()->getMessageFactory(); |
95
|
|
|
|
96
|
27 |
|
foreach ($event->getRequests() as $request) { |
97
|
27 |
|
if (($response = $this->cache->getResponse($request, $messageFactory)) !== null) { |
98
|
9 |
|
$event->addResponse($response); |
99
|
9 |
|
$event->removeRequest($request); |
100
|
25 |
|
} elseif (($exception = $this->cache->getException($request, $messageFactory)) !== null) { |
101
|
9 |
|
$event->addException($exception); |
102
|
13 |
|
$event->removeRequest($request); |
103
|
7 |
|
} |
104
|
21 |
|
} |
105
|
27 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param MultiRequestSentEvent $event |
109
|
|
|
*/ |
110
|
9 |
|
public function onMultiRequestSent(MultiRequestSentEvent $event) |
111
|
|
|
{ |
112
|
9 |
|
foreach ($event->getResponses() as $response) { |
113
|
9 |
|
$this->cache->saveResponse($response, $response->getParameter('request')); |
114
|
7 |
|
} |
115
|
9 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param MultiRequestErroredEvent $event |
119
|
|
|
*/ |
120
|
9 |
|
public function onMultiRequestErrored(MultiRequestErroredEvent $event) |
121
|
|
|
{ |
122
|
9 |
|
foreach ($event->getResponses() as $response) { |
123
|
9 |
|
$this->cache->saveResponse($response, $response->getParameter('request')); |
124
|
7 |
|
} |
125
|
|
|
|
126
|
9 |
|
foreach ($event->getExceptions() as $exception) { |
127
|
9 |
|
$this->cache->saveException($exception, $exception->getRequest()); |
128
|
7 |
|
} |
129
|
9 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
9 |
View Code Duplication |
public static function getSubscribedEvents() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
9 |
|
Events::REQUEST_CREATED => ['onRequestCreated', -100], |
138
|
7 |
|
Events::REQUEST_SENT => ['onRequestSent', -100], |
139
|
7 |
|
Events::REQUEST_ERRORED => ['onRequestErrored', -100], |
140
|
7 |
|
Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', -100], |
141
|
7 |
|
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', -100], |
142
|
7 |
|
Events::MULTI_REQUEST_ERRORED => ['onMultiRequestErrored', -100], |
143
|
7 |
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.