StatusCodeSubscriber::onMultiRequestSent()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
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\RequestSentEvent;
17
use Ivory\HttpAdapter\Event\StatusCode\StatusCode;
18
use Ivory\HttpAdapter\Event\StatusCode\StatusCodeInterface;
19
use Ivory\HttpAdapter\HttpAdapterException;
20
use Ivory\HttpAdapter\HttpAdapterInterface;
21
use Ivory\HttpAdapter\Message\InternalRequestInterface;
22
use Ivory\HttpAdapter\Message\ResponseInterface;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
25
/**
26
 * @author GeLo <[email protected]>
27
 */
28
class StatusCodeSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var StatusCodeInterface
32
     */
33
    private $statusCode;
34
35
    /**
36
     * @param StatusCodeInterface|null $statusCode
37
     */
38 63
    public function __construct(StatusCodeInterface $statusCode = null)
39
    {
40 63
        $this->statusCode = $statusCode ?: new StatusCode();
41 63
    }
42
43
    /**
44
     * @return StatusCodeInterface
45
     */
46 18
    public function getStatusCode()
47
    {
48 18
        return $this->statusCode;
49
    }
50
51
    /**
52
     * @param RequestSentEvent $event
53
     */
54 18
    public function onRequestSent(RequestSentEvent $event)
55
    {
56 18
        if (!$this->statusCode->validate($event->getResponse())) {
0 ignored issues
show
Bug introduced by
It seems like $event->getResponse() can be null; however, validate() 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 9
            $event->setException($this->createStatusCodeException(
58 9
                $event->getResponse(),
0 ignored issues
show
Bug introduced by
It seems like $event->getResponse() can be null; however, createStatusCodeException() 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...
59 9
                $event->getRequest(),
60 9
                $event->getHttpAdapter()
61 7
            ));
62 7
        }
63 18
    }
64
65
    /**
66
     * @param MultiRequestSentEvent $event
67
     */
68 18
    public function onMultiRequestSent(MultiRequestSentEvent $event)
69
    {
70 18
        foreach ($event->getResponses() as $response) {
71 18
            if (!$this->statusCode->validate($response)) {
72 9
                $event->addException($this->createStatusCodeException(
73 7
                    $response,
74 9
                    $response->getParameter('request'),
75 9
                    $event->getHttpAdapter()
76 7
                ));
77
78 11
                $event->removeResponse($response);
79 7
            }
80 14
        }
81 18
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 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...
87
    {
88
        return [
89 9
            Events::REQUEST_SENT       => ['onRequestSent', 200],
90 7
            Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', 200],
91 7
        ];
92
    }
93
94
    /**
95
     * @param ResponseInterface        $response
96
     * @param InternalRequestInterface $internalRequest
97
     * @param HttpAdapterInterface     $httpAdapter
98
     *
99
     * @return HttpAdapterException
100
     */
101 18
    private function createStatusCodeException(
102
        ResponseInterface $response,
103
        InternalRequestInterface $internalRequest,
104
        HttpAdapterInterface $httpAdapter
105
    ) {
106 18
        $exception = HttpAdapterException::cannotFetchUri(
107 18
            (string) $internalRequest->getUri(),
108 18
            $httpAdapter->getName(),
109 18
            sprintf('Status code: %d', $response->getStatusCode())
110 14
        );
111
112 18
        $exception->setRequest($internalRequest);
113 18
        $exception->setResponse($response);
114
115 18
        return $exception;
116
    }
117
}
118