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.

AbstractResource   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildSerializer() 0 4 1
A createApiException() 0 23 3
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
     * Create an ApiException from a client exception.
52
     *
53
     * @param ClientException $e The client exception.
54
     * @return ApiException
55
     * @throws \Speicher210\KontaktIO\Exception\ApiKeyInvalidException
56
     */
57
    protected function createApiException(ClientException $e): ApiException
58
    {
59
        $response = $e->getResponse();
60
61
        if (\in_array($response->getStatusCode(), [401, 403], true)) {
62
            throw ApiKeyInvalidException::forResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $e->getResponse() on line 59 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...
63
        }
64
65
        if ($response->getBody()->getSize() > 0) {
66
            /** @var ApiErrorResponse $apiErrorResponse */
67
            $apiErrorResponse = $this->serializer->deserialize(
68
                $e->getResponse()->getBody(),
69
                ApiErrorResponse::class,
70
                'json'
71
            );
72
        } else {
73
            $apiErrorResponse = new ApiErrorResponse();
74
            $apiErrorResponse->setStatus($response->getStatusCode());
75
            $apiErrorResponse->setMessage($response->getReasonPhrase());
76
        }
77
78
        return new ApiException($apiErrorResponse, $e);
79
    }
80
}
81