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\Events; |
||
15 | use Ivory\HttpAdapter\Event\RequestErroredEvent; |
||
16 | use Ivory\HttpAdapter\Event\MultiRequestErroredEvent; |
||
17 | use Ivory\HttpAdapter\Event\MultiRequestSentEvent; |
||
18 | use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent; |
||
19 | use Ivory\HttpAdapter\Event\RequestCreatedEvent; |
||
20 | use Ivory\HttpAdapter\Event\RequestSentEvent; |
||
21 | use Ivory\HttpAdapter\HttpAdapterInterface; |
||
22 | use Ivory\HttpAdapter\Message\InternalRequestInterface; |
||
23 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
24 | use Symfony\Component\Stopwatch\Stopwatch; |
||
25 | |||
26 | /** |
||
27 | * Stopwatch subscriber. |
||
28 | * |
||
29 | * @author GeLo <[email protected]> |
||
30 | */ |
||
31 | class StopwatchSubscriber implements EventSubscriberInterface |
||
32 | { |
||
33 | /** @var \Symfony\Component\Stopwatch\Stopwatch */ |
||
34 | private $stopwatch; |
||
35 | |||
36 | /** |
||
37 | * Creates a stopwatch event subscriber. |
||
38 | * |
||
39 | * @param \Symfony\Component\Stopwatch\Stopwatch $stopwatch The stopwatch. |
||
40 | */ |
||
41 | 152 | public function __construct(Stopwatch $stopwatch) |
|
42 | { |
||
43 | 152 | $this->stopwatch = $stopwatch; |
|
44 | 152 | } |
|
45 | |||
46 | /** |
||
47 | * Gets the stopwatch. |
||
48 | * |
||
49 | * @return \Symfony\Component\Stopwatch\Stopwatch The stopwatch. |
||
50 | */ |
||
51 | 19 | public function getStopwatch() |
|
52 | { |
||
53 | 19 | return $this->stopwatch; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * On request created event. |
||
58 | * |
||
59 | * @param \Ivory\HttpAdapter\Event\RequestCreatedEvent $event The event. |
||
60 | */ |
||
61 | 19 | public function onRequestCreated(RequestCreatedEvent $event) |
|
62 | { |
||
63 | 19 | $this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest())); |
|
64 | 19 | } |
|
65 | |||
66 | /** |
||
67 | * On request sent event. |
||
68 | * |
||
69 | * @param \Ivory\HttpAdapter\Event\RequestSentEvent $event The event. |
||
70 | */ |
||
71 | 19 | public function onRequestSent(RequestSentEvent $event) |
|
72 | { |
||
73 | 19 | if (!$event->hasException()) { |
|
74 | 19 | $this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest())); |
|
75 | 18 | } |
|
76 | 19 | } |
|
77 | |||
78 | /** |
||
79 | * On request errored event. |
||
80 | * |
||
81 | * @param \Ivory\HttpAdapter\Event\RequestErroredEvent $event The event. |
||
82 | */ |
||
83 | 19 | public function onRequestErrored(RequestErroredEvent $event) |
|
84 | { |
||
85 | 19 | $this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getException()->getRequest())); |
|
86 | 19 | } |
|
87 | |||
88 | /** |
||
89 | * On multi request created event. |
||
90 | * |
||
91 | * @param \Ivory\HttpAdapter\Event\MultiRequestCreatedEvent $event The multi request created event. |
||
92 | */ |
||
93 | 19 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
94 | { |
||
95 | 19 | foreach ($event->getRequests() as $request) { |
|
96 | 19 | $this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $request)); |
|
97 | 18 | } |
|
98 | 19 | } |
|
99 | |||
100 | /** |
||
101 | * On multi request sent event. |
||
102 | * |
||
103 | * @param \Ivory\HttpAdapter\Event\MultiRequestSentEvent $event The multi request sent event. |
||
104 | */ |
||
105 | 19 | public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
106 | { |
||
107 | 19 | foreach ($event->getResponses() as $response) { |
|
108 | 19 | $this->stopwatch->stop( |
|
109 | 19 | $this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request')) |
|
110 | 18 | ); |
|
111 | 18 | } |
|
112 | 19 | } |
|
113 | |||
114 | /** |
||
115 | * On multi request errored event. |
||
116 | * |
||
117 | * @param \Ivory\HttpAdapter\Event\MultiRequestErroredEvent $event The multi request errored event. |
||
118 | */ |
||
119 | 19 | public function onMultiResponseErrored(MultiRequestErroredEvent $event) |
|
120 | { |
||
121 | 19 | foreach ($event->getResponses() as $response) { |
|
122 | 19 | $this->stopwatch->stop( |
|
123 | 19 | $this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request')) |
|
124 | 18 | ); |
|
125 | 18 | } |
|
126 | |||
127 | 19 | foreach ($event->getExceptions() as $exception) { |
|
128 | 19 | $this->stopwatch->stop( |
|
129 | 19 | $this->getStopwatchName($event->getHttpAdapter(), $exception->getRequest()) |
|
130 | 18 | ); |
|
131 | 18 | } |
|
132 | 19 | } |
|
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | 19 | View Code Duplication | public static function getSubscribedEvents() |
0 ignored issues
–
show
|
|||
138 | { |
||
139 | return [ |
||
140 | 19 | Events::REQUEST_CREATED => ['onRequestCreated', 10000], |
|
141 | 18 | Events::REQUEST_SENT => ['onRequestSent', -10000], |
|
142 | 18 | Events::REQUEST_ERRORED => ['onRequestErrored', -10000], |
|
143 | 18 | Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 10000], |
|
144 | 18 | Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', -10000], |
|
145 | 18 | Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', -10000], |
|
146 | 18 | ]; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Gets the stopwatch name. |
||
151 | * |
||
152 | * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. |
||
153 | * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. |
||
154 | * |
||
155 | * @return string The stopwatch name. |
||
156 | */ |
||
157 | 114 | private function getStopwatchName(HttpAdapterInterface $httpAdapter, InternalRequestInterface $internalRequest) |
|
158 | { |
||
159 | 114 | return sprintf('ivory.http_adapter.%s (%s)', $httpAdapter->getName(), (string) $internalRequest->getUri()); |
|
160 | } |
||
161 | } |
||
162 |
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.