Passed
Push — master ( 7a1811...e7b498 )
by Michael
02:30
created

GuzzleAdapter::createException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\HttpClient\Adapter;
5
6
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
7
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
8
use GuzzleHttp\Exception\BadResponseException as GuzzleResponseException;
9
use Mikemirten\Component\JsonApi\HttpClient\Exception\RequestException;
10
use Mikemirten\Component\JsonApi\HttpClient\Exception\ResponseException;
11
use Mikemirten\Component\JsonApi\HttpClient\HttpClientInterface;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Guzzle HTTP Client adapter
17
 *
18
 * @package Mikemirten\Component\JsonApi\HttpClient
19
 */
20
class GuzzleAdapter implements HttpClientInterface
21
{
22
    /**
23
     * @var GuzzleClientInterface
24
     */
25
    protected $client;
26
27
    /**
28
     * GuzzleAdapter constructor.
29
     *
30
     * @param GuzzleClientInterface $client
31
     */
32 3
    public function __construct(GuzzleClientInterface $client)
33
    {
34 3
        $this->client = $client;
35 3
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 3
    public function request(RequestInterface $request): ResponseInterface
41
    {
42
        try {
43 3
            return $this->client->send($request);
44
        }
45 2
        catch (GuzzleRequestException $exception) {
46 2
            throw $this->createException($exception);
47
        }
48
    }
49
50
    /**
51
     * Create HTTP-Client RequestException by a Guzzle exception
52
     *
53
     * @param  GuzzleRequestException $exception
54
     * @return RequestException
55
     */
56 2
    protected function createException(GuzzleRequestException $exception): RequestException
57
    {
58 2
        if ($exception instanceof GuzzleResponseException) {
59 1
            return new ResponseException(
60 1
                $exception->getRequest(),
61 1
                $exception->getResponse(),
0 ignored issues
show
Bug introduced by
It seems like $exception->getResponse() can be null; however, __construct() 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...
62 1
                $exception
63
            );
64
        }
65
66 1
        throw new RequestException(
67 1
            $exception->getRequest(),
68 1
            $exception
69
        );
70
    }
71
}