Completed
Pull Request — master (#129)
by Eric
257:01 queued 192:06
created

src/Event/Subscriber/CookieSubscriber.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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

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.

Loading history...
130
    {
131
        return array(
132 19
            Events::REQUEST_CREATED       => array('onRequestCreated', 300),
133 18
            Events::REQUEST_SENT          => array('onRequestSent', 300),
134 18
            Events::REQUEST_ERRORED       => array('onRequestErrored', 300),
135 18
            Events::MULTI_REQUEST_CREATED => array('onMultiRequestCreated', 300),
136 18
            Events::MULTI_REQUEST_SENT    => array('onMultiRequestSent', 300),
137 18
            Events::MULTI_REQUEST_ERRORED => array('onMultiResponseErrored', 300),
138 18
        );
139
    }
140
}
141