-
Notifications
You must be signed in to change notification settings - Fork 19
/
StopwatchSubscriber.php
143 lines (128 loc) · 4.21 KB
/
StopwatchSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
* This file is part of the Ivory Http Adapter package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/
namespace Ivory\HttpAdapter\Event\Subscriber;
use Ivory\HttpAdapter\Event\Events;
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent;
use Ivory\HttpAdapter\Event\MultiRequestErroredEvent;
use Ivory\HttpAdapter\Event\MultiRequestSentEvent;
use Ivory\HttpAdapter\Event\RequestCreatedEvent;
use Ivory\HttpAdapter\Event\RequestErroredEvent;
use Ivory\HttpAdapter\Event\RequestSentEvent;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Ivory\HttpAdapter\Message\InternalRequestInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Stopwatch\Stopwatch;
/**
* @author GeLo <geloen.eric@gmail.com>
*/
class StopwatchSubscriber implements EventSubscriberInterface
{
/**
* @var Stopwatch
*/
private $stopwatch;
/**
* @param Stopwatch $stopwatch
*/
public function __construct(Stopwatch $stopwatch)
{
$this->stopwatch = $stopwatch;
}
/**
* @return Stopwatch
*/
public function getStopwatch()
{
return $this->stopwatch;
}
/**
* @param RequestCreatedEvent $event
*/
public function onRequestCreated(RequestCreatedEvent $event)
{
$this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest()));
}
/**
* @param RequestSentEvent $event
*/
public function onRequestSent(RequestSentEvent $event)
{
if (!$event->hasException()) {
$this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest()));
}
}
/**
* @param RequestErroredEvent $event
*/
public function onRequestErrored(RequestErroredEvent $event)
{
$this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getException()->getRequest()));
}
/**
* @param MultiRequestCreatedEvent $event
*/
public function onMultiRequestCreated(MultiRequestCreatedEvent $event)
{
foreach ($event->getRequests() as $request) {
$this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $request));
}
}
/**
* @param MultiRequestSentEvent $event
*/
public function onMultiRequestSent(MultiRequestSentEvent $event)
{
foreach ($event->getResponses() as $response) {
$this->stopwatch->stop(
$this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request'))
);
}
}
/**
* @param MultiRequestErroredEvent $event
*/
public function onMultiResponseErrored(MultiRequestErroredEvent $event)
{
foreach ($event->getResponses() as $response) {
$this->stopwatch->stop(
$this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request'))
);
}
foreach ($event->getExceptions() as $exception) {
$this->stopwatch->stop(
$this->getStopwatchName($event->getHttpAdapter(), $exception->getRequest())
);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Events::REQUEST_CREATED => ['onRequestCreated', 10000],
Events::REQUEST_SENT => ['onRequestSent', -10000],
Events::REQUEST_ERRORED => ['onRequestErrored', -10000],
Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 10000],
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', -10000],
Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', -10000],
];
}
/**
* @param HttpAdapterInterface $httpAdapter
* @param InternalRequestInterface $internalRequest
*
* @return string
*/
private function getStopwatchName(HttpAdapterInterface $httpAdapter, InternalRequestInterface $internalRequest)
{
return sprintf('ivory.http_adapter.%s (%s)', $httpAdapter->getName(), (string) $internalRequest->getUri());
}
}