RedirectSubscriber   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 7
loc 105
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getRedirect() 0 4 1
B onRequestSent() 0 26 4
B onMultiRequestSent() 0 37 6
A getSubscribedEvents() 7 7 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\Events;
15
use Ivory\HttpAdapter\Event\MultiRequestSentEvent;
16
use Ivory\HttpAdapter\Event\Redirect\Redirect;
17
use Ivory\HttpAdapter\Event\Redirect\RedirectInterface;
18
use Ivory\HttpAdapter\Event\RequestSentEvent;
19
use Ivory\HttpAdapter\HttpAdapterException;
20
use Ivory\HttpAdapter\MultiHttpAdapterException;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
/**
24
 * @author GeLo <[email protected]>
25
 */
26
class RedirectSubscriber implements EventSubscriberInterface
27
{
28
    /**
29
     * @var RedirectInterface
30
     */
31
    private $redirect;
32
33
    /**
34
     * @param RedirectInterface $redirect
35
     */
36 99
    public function __construct(RedirectInterface $redirect = null)
37
    {
38 99
        $this->redirect = $redirect ?: new Redirect();
39 99
    }
40
41
    /**
42
     * @return RedirectInterface
43
     */
44 18
    public function getRedirect()
45
    {
46 18
        return $this->redirect;
47
    }
48
49
    /**
50
     * @param RequestSentEvent $event
51
     */
52 36
    public function onRequestSent(RequestSentEvent $event)
53
    {
54
        try {
55 36
            $redirectRequest = $this->redirect->createRedirectRequest(
56 36
                $event->getResponse(),
0 ignored issues
show
Bug introduced by Eric GELOEN
It seems like $event->getResponse() can be null; however, createRedirectRequest() 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...
57 36
                $event->getRequest(),
58 36
                $event->getHttpAdapter()
59 28
            );
60 30
        } catch (HttpAdapterException $e) {
61 9
            $event->setException($e);
62
63 9
            return;
64
        }
65
66 27
        if ($redirectRequest === false) {
67 9
            $event->setResponse($this->redirect->prepareResponse($event->getResponse(), $event->getRequest()));
0 ignored issues
show
Bug introduced by Eric GELOEN
It seems like $event->getResponse() can be null; however, prepareResponse() 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...
68
69 9
            return;
70
        }
71
72
        try {
73 18
            $event->setResponse($event->getHttpAdapter()->sendRequest($redirectRequest));
74 16
        } catch (HttpAdapterException $e) {
75 9
            $event->setException($e);
76
        }
77 18
    }
78
79
    /**
80
     * @param MultiRequestSentEvent $event
81
     */
82 36
    public function onMultiRequestSent(MultiRequestSentEvent $event)
83
    {
84 36
        $redirectRequests = [];
85
86 36
        foreach ($event->getResponses() as $response) {
87
            try {
88 36
                $redirectRequest = $this->redirect->createRedirectRequest(
89 28
                    $response,
90 36
                    $response->getParameter('request'),
91 36
                    $event->getHttpAdapter()
92 28
                );
93 30
            } catch (HttpAdapterException $e) {
94 9
                $event->removeResponse($response);
95 9
                $event->addException($e);
96 9
                continue;
97
            }
98
99 27
            if ($redirectRequest === false) {
100 9
                $event->removeResponse($response);
101 9
                $event->addResponse($this->redirect->prepareResponse($response, $response->getParameter('request')));
102 7
            } else {
103 18
                $redirectRequests[] = $redirectRequest;
104 20
                $event->removeResponse($response);
105
            }
106 28
        }
107
108 36
        if (empty($redirectRequests)) {
109 18
            return;
110
        }
111
112
        try {
113 18
            $event->addResponses($event->getHttpAdapter()->sendRequests($redirectRequests));
114 16
        } catch (MultiHttpAdapterException $e) {
115 9
            $event->addResponses($e->getResponses());
116 9
            $event->addExceptions($e->getExceptions());
117
        }
118 18
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 9 View Code Duplication
    public static function getSubscribedEvents()
0 ignored issues
show
Duplication introduced by Eric GELOEN
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...
124
    {
125
        return [
126 9
            Events::REQUEST_SENT       => ['onRequestSent', 0],
127 7
            Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', 0],
128 7
        ];
129
    }
130
}
131