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 ( 7d318e...636af9 )
by Dragos
32:49 queued 17:45
created

AbstractResource::createApiException()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace Speicher210\KontaktIO;
4
5
use GuzzleHttp\Exception\ClientException;
6
use JMS\Serializer\SerializerInterface;
7
use Speicher210\KontaktIO\Exception\ApiException;
8
use Speicher210\KontaktIO\Exception\ApiKeyInvalidException;
9
use Speicher210\KontaktIO\Model\ApiErrorResponse;
10
11
/**
12
 * Abstract resource.
13
 */
14
class AbstractResource
15
{
16
    /**
17
     * The API client.
18
     *
19
     * @var Client
20
     */
21
    protected $client;
22
23
    /**
24
     * Serializer interface to serialize / deserialize the request / responses.
25
     *
26
     * @var SerializerInterface
27
     */
28
    protected $serializer;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param Client $client The API client.
34
     * @param SerializerInterface $serializer Serializer interface to serialize / deserialize the request / responses.
35
     */
36
    public function __construct(Client $client, SerializerInterface $serializer)
37
    {
38
        $this->client = $client;
39
        $this->serializer = $serializer;
40
    }
41
42
    /**
43
     * Create an ApiException from a client exception.
44
     *
45
     * @param ClientException $e The client exception.
46
     * @return ApiException
47
     */
48
    protected function createApiException(ClientException $e)
49
    {
50
        $response = $e->getResponse();
51
52
        if ($response->getStatusCode() === 401 || $response->getStatusCode() === 403) {
53
            throw new ApiKeyInvalidException($response);
54
        }
55
56
        if ($response->getBody()->getSize() > 0) {
57
            /** @var ApiErrorResponse $apiErrorResponse */
58
            $apiErrorResponse = $this->serializer->deserialize(
59
                $e->getResponse()->getBody(),
60
                ApiErrorResponse::class,
61
                'json'
62
            );
63
        } else {
64
            $apiErrorResponse = new ApiErrorResponse();
65
            $apiErrorResponse->setStatus($response->getStatusCode());
66
            $apiErrorResponse->setMessage($response->getReasonPhrase());
67
        }
68
69
        return new ApiException($apiErrorResponse, $e);
70
    }
71
}
72