Completed
Pull Request — master (#91)
by Timothée
07:01
created

StatusCodeVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tolerance package.
5
 *
6
 * (c) Samuel ROZE <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tolerance\Bridge\PhpHttp;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Tolerance\Operation\Exception\PromiseException;
16
use Tolerance\Operation\ExceptionCatcher\ThrowableCatcherVoter;
17
18
final class StatusCodeVoter implements ThrowableCatcherVoter
19
{
20
    private $statusCodes;
21
22
    public function __construct(array $statusCodes)
23
    {
24
        $this->statusCodes = $statusCodes;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function shouldCatch(\Exception $e)
31
    {
32
        return $this->shouldCatchThrowable($e);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function shouldCatchThrowable($throwable)
39
    {
40
        return !$throwable instanceof PromiseException
41
            || $throwable->isRejected()
42
            || !$throwable->getValue() instanceof ResponseInterface
43
            || in_array($throwable->getValue()->getStatusCode(), $this->statusCodes)
44
        ;
45
    }
46
}
47