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\Redirect\Redirect; |
17
|
|
|
use Ivory\HttpAdapter\Event\Redirect\RedirectInterface; |
18
|
|
|
use Ivory\HttpAdapter\Event\RequestSentEvent; |
19
|
|
|
use Ivory\HttpAdapter\HttpAdapterException; |
20
|
|
|
use Ivory\HttpAdapter\MultiHttpAdapterException; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author GeLo <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class RedirectSubscriber implements EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RedirectInterface |
30
|
|
|
*/ |
31
|
|
|
private $redirect; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RedirectInterface $redirect |
35
|
|
|
*/ |
36
|
99 |
|
public function __construct(RedirectInterface $redirect = null) |
37
|
|
|
{ |
38
|
99 |
|
$this->redirect = $redirect ?: new Redirect(); |
39
|
99 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return RedirectInterface |
43
|
|
|
*/ |
44
|
18 |
|
public function getRedirect() |
45
|
|
|
{ |
46
|
18 |
|
return $this->redirect; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param RequestSentEvent $event |
51
|
|
|
*/ |
52
|
36 |
|
public function onRequestSent(RequestSentEvent $event) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
36 |
|
$redirectRequest = $this->redirect->createRedirectRequest( |
56
|
36 |
|
$event->getResponse(), |
|
|
|
|
57
|
36 |
|
$event->getRequest(), |
58
|
36 |
|
$event->getHttpAdapter() |
59
|
28 |
|
); |
60
|
30 |
|
} catch (HttpAdapterException $e) { |
61
|
9 |
|
$event->setException($e); |
62
|
|
|
|
63
|
9 |
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
27 |
|
if ($redirectRequest === false) { |
67
|
9 |
|
$event->setResponse($this->redirect->prepareResponse($event->getResponse(), $event->getRequest())); |
|
|
|
|
68
|
|
|
|
69
|
9 |
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
18 |
|
$event->setResponse($event->getHttpAdapter()->sendRequest($redirectRequest)); |
74
|
16 |
|
} catch (HttpAdapterException $e) { |
75
|
9 |
|
$event->setException($e); |
76
|
|
|
} |
77
|
18 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param MultiRequestSentEvent $event |
81
|
|
|
*/ |
82
|
36 |
|
public function onMultiRequestSent(MultiRequestSentEvent $event) |
83
|
|
|
{ |
84
|
36 |
|
$redirectRequests = []; |
85
|
|
|
|
86
|
36 |
|
foreach ($event->getResponses() as $response) { |
87
|
|
|
try { |
88
|
36 |
|
$redirectRequest = $this->redirect->createRedirectRequest( |
89
|
28 |
|
$response, |
90
|
36 |
|
$response->getParameter('request'), |
91
|
36 |
|
$event->getHttpAdapter() |
92
|
28 |
|
); |
93
|
30 |
|
} catch (HttpAdapterException $e) { |
94
|
9 |
|
$event->removeResponse($response); |
95
|
9 |
|
$event->addException($e); |
96
|
9 |
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
27 |
|
if ($redirectRequest === false) { |
100
|
9 |
|
$event->removeResponse($response); |
101
|
9 |
|
$event->addResponse($this->redirect->prepareResponse($response, $response->getParameter('request'))); |
102
|
7 |
|
} else { |
103
|
18 |
|
$redirectRequests[] = $redirectRequest; |
104
|
20 |
|
$event->removeResponse($response); |
105
|
|
|
} |
106
|
28 |
|
} |
107
|
|
|
|
108
|
36 |
|
if (empty($redirectRequests)) { |
109
|
18 |
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
try { |
113
|
18 |
|
$event->addResponses($event->getHttpAdapter()->sendRequests($redirectRequests)); |
114
|
16 |
|
} catch (MultiHttpAdapterException $e) { |
115
|
9 |
|
$event->addResponses($e->getResponses()); |
116
|
9 |
|
$event->addExceptions($e->getExceptions()); |
117
|
|
|
} |
118
|
18 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
9 |
View Code Duplication |
public static function getSubscribedEvents() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
9 |
|
Events::REQUEST_SENT => ['onRequestSent', 0], |
127
|
7 |
|
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', 0], |
128
|
7 |
|
]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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: