-
Notifications
You must be signed in to change notification settings - Fork 19
/
CookieSubscriber.php
124 lines (111 loc) · 3.54 KB
/
CookieSubscriber.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
<?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\Cookie\Jar\CookieJar;
use Ivory\HttpAdapter\Event\Cookie\Jar\CookieJarInterface;
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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @author GeLo <geloen.eric@gmail.com>
*/
class CookieSubscriber implements EventSubscriberInterface
{
/**
* @var CookieJarInterface
*/
private $cookieJar;
/**
* @param CookieJarInterface|null $cookieJar
*/
public function __construct(CookieJarInterface $cookieJar = null)
{
$this->cookieJar = $cookieJar ?: new CookieJar();
}
/**
* @return CookieJarInterface
*/
public function getCookieJar()
{
return $this->cookieJar;
}
/**
* @param RequestCreatedEvent $event
*/
public function onRequestCreated(RequestCreatedEvent $event)
{
$event->setRequest($this->cookieJar->populate($event->getRequest()));
}
/**
* @param RequestSentEvent $event
*/
public function onRequestSent(RequestSentEvent $event)
{
$this->cookieJar->extract($event->getRequest(), $event->getResponse());
}
/**
* @param RequestErroredEvent $event
*/
public function onRequestErrored(RequestErroredEvent $event)
{
if ($event->getException()->hasResponse()) {
$this->cookieJar->extract($event->getException()->getRequest(), $event->getException()->getResponse());
}
}
/**
* @param MultiRequestCreatedEvent $event
*/
public function onMultiRequestCreated(MultiRequestCreatedEvent $event)
{
foreach ($event->getRequests() as $request) {
$event->removeRequest($request);
$event->addRequest($this->cookieJar->populate($request));
}
}
/**
* @param MultiRequestSentEvent $event
*/
public function onMultiRequestSent(MultiRequestSentEvent $event)
{
foreach ($event->getResponses() as $response) {
$this->cookieJar->extract($response->getParameter('request'), $response);
}
}
/**
* @param MultiRequestErroredEvent $event
*/
public function onMultiResponseErrored(MultiRequestErroredEvent $event)
{
foreach ($event->getExceptions() as $exception) {
if ($exception->hasResponse()) {
$this->cookieJar->extract($exception->getRequest(), $exception->getResponse());
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Events::REQUEST_CREATED => ['onRequestCreated', 300],
Events::REQUEST_SENT => ['onRequestSent', 300],
Events::REQUEST_ERRORED => ['onRequestErrored', 300],
Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 300],
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', 300],
Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', 300],
];
}
}