CookieSubscriber::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
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());
0 ignored issues
show
Bug introduced by
It seems like $event->getResponse() can be null; however, extract() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $event->getException()->getRequest() can be null; however, extract() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $event->getException()->getResponse() can be null; however, extract() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $exception->getRequest() can be null; however, extract() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $exception->getResponse() can be null; however, extract() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
106 7
            }
107 7
        }
108 9
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 9 View Code Duplication
    public static function getSubscribedEvents()
0 ignored issues
show
Duplication introduced by
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...
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