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\MultiRequestSentEvent; |
16
|
|
|
use Ivory\HttpAdapter\Event\RequestSentEvent; |
17
|
|
|
use Ivory\HttpAdapter\Event\StatusCode\StatusCode; |
18
|
|
|
use Ivory\HttpAdapter\Event\StatusCode\StatusCodeInterface; |
19
|
|
|
use Ivory\HttpAdapter\HttpAdapterException; |
20
|
|
|
use Ivory\HttpAdapter\HttpAdapterInterface; |
21
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
22
|
|
|
use Ivory\HttpAdapter\Message\ResponseInterface; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class StatusCodeSubscriber implements EventSubscriberInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var StatusCodeInterface |
32
|
|
|
*/ |
33
|
|
|
private $statusCode; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param StatusCodeInterface|null $statusCode |
37
|
|
|
*/ |
38
|
63 |
|
public function __construct(StatusCodeInterface $statusCode = null) |
39
|
|
|
{ |
40
|
63 |
|
$this->statusCode = $statusCode ?: new StatusCode(); |
41
|
63 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return StatusCodeInterface |
45
|
|
|
*/ |
46
|
18 |
|
public function getStatusCode() |
47
|
|
|
{ |
48
|
18 |
|
return $this->statusCode; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param RequestSentEvent $event |
53
|
|
|
*/ |
54
|
18 |
|
public function onRequestSent(RequestSentEvent $event) |
55
|
|
|
{ |
56
|
18 |
|
if (!$this->statusCode->validate($event->getResponse())) { |
|
|
|
|
57
|
9 |
|
$event->setException($this->createStatusCodeException( |
58
|
9 |
|
$event->getResponse(), |
|
|
|
|
59
|
9 |
|
$event->getRequest(), |
60
|
9 |
|
$event->getHttpAdapter() |
61
|
7 |
|
)); |
62
|
7 |
|
} |
63
|
18 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param MultiRequestSentEvent $event |
67
|
|
|
*/ |
68
|
18 |
|
public function onMultiRequestSent(MultiRequestSentEvent $event) |
69
|
|
|
{ |
70
|
18 |
|
foreach ($event->getResponses() as $response) { |
71
|
18 |
|
if (!$this->statusCode->validate($response)) { |
72
|
9 |
|
$event->addException($this->createStatusCodeException( |
73
|
7 |
|
$response, |
74
|
9 |
|
$response->getParameter('request'), |
75
|
9 |
|
$event->getHttpAdapter() |
76
|
7 |
|
)); |
77
|
|
|
|
78
|
11 |
|
$event->removeResponse($response); |
79
|
7 |
|
} |
80
|
14 |
|
} |
81
|
18 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
9 |
View Code Duplication |
public static function getSubscribedEvents() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
9 |
|
Events::REQUEST_SENT => ['onRequestSent', 200], |
90
|
7 |
|
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', 200], |
91
|
7 |
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ResponseInterface $response |
96
|
|
|
* @param InternalRequestInterface $internalRequest |
97
|
|
|
* @param HttpAdapterInterface $httpAdapter |
98
|
|
|
* |
99
|
|
|
* @return HttpAdapterException |
100
|
|
|
*/ |
101
|
18 |
|
private function createStatusCodeException( |
102
|
|
|
ResponseInterface $response, |
103
|
|
|
InternalRequestInterface $internalRequest, |
104
|
|
|
HttpAdapterInterface $httpAdapter |
105
|
|
|
) { |
106
|
18 |
|
$exception = HttpAdapterException::cannotFetchUri( |
107
|
18 |
|
(string) $internalRequest->getUri(), |
108
|
18 |
|
$httpAdapter->getName(), |
109
|
18 |
|
sprintf('Status code: %d', $response->getStatusCode()) |
110
|
14 |
|
); |
111
|
|
|
|
112
|
18 |
|
$exception->setRequest($internalRequest); |
113
|
18 |
|
$exception->setResponse($response); |
114
|
|
|
|
115
|
18 |
|
return $exception; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: