GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2a1f44...ecf505 )
by Dragos
12:25
created

AbstractResource::buildSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Speicher210\KontaktIO;
6
7
use GuzzleHttp\Exception\ClientException;
8
use JMS\Serializer\SerializerBuilder;
9
use JMS\Serializer\SerializerInterface;
10
use Speicher210\KontaktIO\Exception\ApiException;
11
use Speicher210\KontaktIO\Exception\ApiKeyInvalidException;
12
use Speicher210\KontaktIO\Model\ApiErrorResponse;
13
14
/**
15
 * Abstract resource.
16
 */
17
class AbstractResource
18
{
19
    /**
20
     * The API client.
21
     *
22
     * @var Client
23
     */
24
    protected $client;
25
26
    /**
27
     * Serializer interface to serialize / deserialize the request / responses.
28
     *
29
     * @var SerializerInterface
30
     */
31
    protected $serializer;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Client $client The API client.
37
     * @param SerializerInterface $serializer Serializer interface to serialize / deserialize the request / responses.
38
     */
39
    public function __construct(Client $client, SerializerInterface $serializer = null)
40
    {
41
        $this->client = $client;
42
        $this->serializer = $serializer ?? $this->buildSerializer();
43
    }
44
45
    private function buildSerializer(): SerializerInterface
46
    {
47
        return SerializerBuilder::create()->build();
48
    }
49
50
51
    /**
52
     * Create an ApiException from a client exception.
53
     *
54
     * @param ClientException $e The client exception.
55
     * @return ApiException
56
     * @throws \Speicher210\KontaktIO\Exception\ApiKeyInvalidException
57
     */
58
    protected function createApiException(ClientException $e): ApiException
59
    {
60
        $response = $e->getResponse();
61
62
        if (\in_array($response->getStatusCode(), [401, 403], true)) {
63
            throw ApiKeyInvalidException::forResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $e->getResponse() on line 60 can be null; however, Speicher210\KontaktIO\Ex...xception::forResponse() 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...
64
        }
65
66
        if ($response->getBody()->getSize() > 0) {
67
            /** @var ApiErrorResponse $apiErrorResponse */
68
            $apiErrorResponse = $this->serializer->deserialize(
69
                $e->getResponse()->getBody(),
70
                ApiErrorResponse::class,
71
                'json'
72
            );
73
        } else {
74
            $apiErrorResponse = new ApiErrorResponse();
75
            $apiErrorResponse->setStatus($response->getStatusCode());
76
            $apiErrorResponse->setMessage($response->getReasonPhrase());
77
        }
78
79
        return new ApiException($apiErrorResponse, $e);
80
    }
81
}
82