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\Cookie\Jar\CookieJar; |
15
|
|
|
use Ivory\HttpAdapter\Event\Cookie\Jar\CookieJarInterface; |
16
|
|
|
use Ivory\HttpAdapter\Event\Events; |
17
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent; |
18
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestErroredEvent; |
19
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestSentEvent; |
20
|
|
|
use Ivory\HttpAdapter\Event\RequestCreatedEvent; |
21
|
|
|
use Ivory\HttpAdapter\Event\RequestErroredEvent; |
22
|
|
|
use Ivory\HttpAdapter\Event\RequestSentEvent; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class CookieSubscriber implements EventSubscriberInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var CookieJarInterface |
32
|
|
|
*/ |
33
|
|
|
private $cookieJar; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param CookieJarInterface|null $cookieJar |
37
|
|
|
*/ |
38
|
72 |
|
public function __construct(CookieJarInterface $cookieJar = null) |
39
|
|
|
{ |
40
|
72 |
|
$this->cookieJar = $cookieJar ?: new CookieJar(); |
41
|
72 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return CookieJarInterface |
45
|
|
|
*/ |
46
|
9 |
|
public function getCookieJar() |
47
|
|
|
{ |
48
|
9 |
|
return $this->cookieJar; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param RequestCreatedEvent $event |
53
|
|
|
*/ |
54
|
9 |
|
public function onRequestCreated(RequestCreatedEvent $event) |
55
|
|
|
{ |
56
|
9 |
|
$event->setRequest($this->cookieJar->populate($event->getRequest())); |
57
|
9 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param RequestSentEvent $event |
61
|
|
|
*/ |
62
|
9 |
|
public function onRequestSent(RequestSentEvent $event) |
63
|
|
|
{ |
64
|
9 |
|
$this->cookieJar->extract($event->getRequest(), $event->getResponse()); |
|
|
|
|
65
|
9 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param RequestErroredEvent $event |
69
|
|
|
*/ |
70
|
9 |
|
public function onRequestErrored(RequestErroredEvent $event) |
71
|
|
|
{ |
72
|
9 |
|
if ($event->getException()->hasResponse()) { |
73
|
9 |
|
$this->cookieJar->extract($event->getException()->getRequest(), $event->getException()->getResponse()); |
|
|
|
|
74
|
7 |
|
} |
75
|
9 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param MultiRequestCreatedEvent $event |
79
|
|
|
*/ |
80
|
9 |
|
public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
81
|
|
|
{ |
82
|
9 |
|
foreach ($event->getRequests() as $request) { |
83
|
9 |
|
$event->removeRequest($request); |
84
|
9 |
|
$event->addRequest($this->cookieJar->populate($request)); |
85
|
7 |
|
} |
86
|
9 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param MultiRequestSentEvent $event |
90
|
|
|
*/ |
91
|
9 |
|
public function onMultiRequestSent(MultiRequestSentEvent $event) |
92
|
|
|
{ |
93
|
9 |
|
foreach ($event->getResponses() as $response) { |
94
|
9 |
|
$this->cookieJar->extract($response->getParameter('request'), $response); |
95
|
7 |
|
} |
96
|
9 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param MultiRequestErroredEvent $event |
100
|
|
|
*/ |
101
|
9 |
|
public function onMultiResponseErrored(MultiRequestErroredEvent $event) |
102
|
|
|
{ |
103
|
9 |
|
foreach ($event->getExceptions() as $exception) { |
104
|
9 |
|
if ($exception->hasResponse()) { |
105
|
9 |
|
$this->cookieJar->extract($exception->getRequest(), $exception->getResponse()); |
|
|
|
|
106
|
7 |
|
} |
107
|
7 |
|
} |
108
|
9 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
9 |
View Code Duplication |
public static function getSubscribedEvents() |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
9 |
|
Events::REQUEST_CREATED => ['onRequestCreated', 300], |
117
|
7 |
|
Events::REQUEST_SENT => ['onRequestSent', 300], |
118
|
7 |
|
Events::REQUEST_ERRORED => ['onRequestErrored', 300], |
119
|
7 |
|
Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 300], |
120
|
7 |
|
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', 300], |
121
|
7 |
|
Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', 300], |
122
|
7 |
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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: