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\History\Journal; |
16
|
|
|
use Ivory\HttpAdapter\Event\History\JournalInterface; |
17
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent; |
18
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestSentEvent; |
19
|
|
|
use Ivory\HttpAdapter\Event\RequestCreatedEvent; |
20
|
|
|
use Ivory\HttpAdapter\Event\RequestSentEvent; |
21
|
|
|
use Ivory\HttpAdapter\Event\Timer\TimerInterface; |
22
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
23
|
|
|
use Ivory\HttpAdapter\Message\ResponseInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class HistorySubscriber extends AbstractTimerSubscriber |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var JournalInterface |
32
|
|
|
*/ |
33
|
|
|
private $journal; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param JournalInterface|null $journal |
37
|
|
|
* @param TimerInterface|null $timer |
38
|
|
|
*/ |
39
|
45 |
|
public function __construct(JournalInterface $journal = null, TimerInterface $timer = null) |
40
|
|
|
{ |
41
|
45 |
|
parent::__construct($timer); |
42
|
|
|
|
43
|
45 |
|
$this->journal = $journal ?: new Journal(); |
44
|
45 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return JournalInterface |
48
|
|
|
*/ |
49
|
18 |
|
public function getJournal() |
50
|
|
|
{ |
51
|
18 |
|
return $this->journal; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param RequestCreatedEvent $event |
56
|
|
|
*/ |
57
|
9 |
|
public function onRequestCreated(RequestCreatedEvent $event) |
58
|
|
|
{ |
59
|
9 |
|
$event->setRequest($this->getTimer()->start($event->getRequest())); |
60
|
9 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param RequestSentEvent $event |
64
|
|
|
*/ |
65
|
9 |
|
public function onRequestSent(RequestSentEvent $event) |
66
|
|
|
{ |
67
|
9 |
|
$event->setRequest($this->record($event->getRequest(), $event->getResponse())); |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param MultiRequestCreatedEvent $event |
72
|
|
|
*/ |
73
|
9 |
|
public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
74
|
|
|
{ |
75
|
9 |
|
foreach ($event->getRequests() as $request) { |
76
|
9 |
|
$event->removeRequest($request); |
77
|
9 |
|
$event->addRequest($this->getTimer()->start($request)); |
78
|
7 |
|
} |
79
|
9 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param MultiRequestSentEvent $event |
83
|
|
|
*/ |
84
|
9 |
View Code Duplication |
public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
|
|
|
85
|
|
|
{ |
86
|
9 |
|
foreach ($event->getResponses() as $response) { |
87
|
9 |
|
$request = $this->record($response->getParameter('request'), $response); |
88
|
|
|
|
89
|
9 |
|
$event->removeResponse($response); |
90
|
9 |
|
$event->addResponse($response->withParameter('request', $request)); |
91
|
7 |
|
} |
92
|
9 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
9 |
|
public static function getSubscribedEvents() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
9 |
|
Events::REQUEST_CREATED => ['onRequestCreated', 100], |
101
|
7 |
|
Events::REQUEST_SENT => ['onRequestSent', 100], |
102
|
7 |
|
Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 100], |
103
|
7 |
|
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', 100], |
104
|
7 |
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param InternalRequestInterface $internalRequest |
109
|
|
|
* @param ResponseInterface $response |
110
|
|
|
* |
111
|
|
|
* @return InternalRequestInterface |
112
|
|
|
*/ |
113
|
18 |
|
private function record(InternalRequestInterface $internalRequest, ResponseInterface $response) |
114
|
|
|
{ |
115
|
18 |
|
$internalRequest = $this->getTimer()->stop($internalRequest); |
116
|
18 |
|
$this->journal->record($internalRequest, $response); |
117
|
|
|
|
118
|
18 |
|
return $internalRequest; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.