CookieSubscriber   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 11.34 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 10
dl 11
loc 97
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getCookieJar() 0 4 1
A onRequestCreated() 0 4 1
A onRequestSent() 0 4 1
A onRequestErrored() 0 6 2
A onMultiRequestCreated() 0 7 2
A onMultiRequestSent() 0 6 2
A onMultiResponseErrored() 0 8 3
A getSubscribedEvents() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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