RetrySubscriber::onMultiResponseErrored()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 9
nop 1
crap 5
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\MultiRequestErroredEvent;
16
use Ivory\HttpAdapter\Event\RequestErroredEvent;
17
use Ivory\HttpAdapter\Event\Retry\Retry;
18
use Ivory\HttpAdapter\Event\Retry\RetryInterface;
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 RetrySubscriber implements EventSubscriberInterface
27
{
28
    /**
29
     * @var RetryInterface
30
     */
31
    private $retry;
32
33
    /**
34
     * @param RetryInterface|null $retry
35
     */
36 81
    public function __construct(RetryInterface $retry = null)
37
    {
38 81
        $this->retry = $retry ?: new Retry();
39 81
    }
40
41
    /**
42
     * @return RetryInterface
43
     */
44 18
    public function getRetry()
45
    {
46 18
        return $this->retry;
47
    }
48
49
    /**
50
     * @param RequestErroredEvent $event
51
     */
52 27
    public function onRequestErrored(RequestErroredEvent $event)
53
    {
54 27
        if (($request = $this->retry->retry($event->getException()->getRequest())) === false) {
0 ignored issues
show
Bug introduced by
It seems like $event->getException()->getRequest() can be null; however, retry() 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...
55 9
            return;
56
        }
57
58 18
        $event->getException()->setRequest($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->retry->retry($eve...eption()->getRequest()) on line 54 can also be of type boolean; however, Ivory\HttpAdapter\HttpAd...Exception::setRequest() does only seem to accept null|object<Ivory\HttpAd...ternalRequestInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
60
        try {
61 18
            $event->setResponse($event->getHttpAdapter()->sendRequest($request));
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->retry->retry($eve...eption()->getRequest()) on line 54 can also be of type boolean; however, Ivory\HttpAdapter\PsrHtt...nterface::sendRequest() does only seem to accept object<Psr\Http\Message\RequestInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
62 16
        } catch (HttpAdapterException $e) {
63 9
            $event->setException($e);
64
        }
65 18
    }
66
67
    /**
68
     * @param MultiRequestErroredEvent $event
69
     */
70 27
    public function onMultiResponseErrored(MultiRequestErroredEvent $event)
71
    {
72 27
        $retryRequests = [];
73
74 27
        foreach ($event->getExceptions() as $exception) {
75 27
            if (($request = $this->retry->retry($exception->getRequest(), false)) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $exception->getRequest() can be null; however, retry() 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...
76 18
                $retryRequests[] = $request;
77 20
                $event->removeException($exception);
78 14
            }
79 21
        }
80
81 27
        if (empty($retryRequests)) {
82 9
            return;
83
        }
84
85
        try {
86 18
            $event->addResponses($event->getHttpAdapter()->sendRequests($retryRequests));
87 16
        } catch (MultiHttpAdapterException $e) {
88 9
            $event->addResponses($e->getResponses());
89 9
            $event->addExceptions($e->getExceptions());
90
        }
91 18
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 9
    public static function getSubscribedEvents()
97
    {
98
        return [
99 9
            Events::REQUEST_ERRORED       => ['onRequestErrored', 0],
100 7
            Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', 0],
101 7
        ];
102
    }
103
}
104