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.

ServerClient::responseHandler()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 3
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Common\HttpClient;
11
12
use SURFnet\VPN\Common\HttpClient\Exception\ApiException;
13
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException;
14
15
class ServerClient
16
{
17
    /** @var HttpClientInterface */
18
    private $httpClient;
19
20
    /** @var string */
21
    private $baseUri;
22
23
    public function __construct(HttpClientInterface $httpClient, $baseUri)
24
    {
25
        $this->httpClient = $httpClient;
26
        $this->baseUri = $baseUri;
27
    }
28
29
    public function get($requestPath, array $getData = [])
30
    {
31
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);
32
        if (0 !== count($getData)) {
33
            $requestUri = sprintf('%s?%s', $requestUri, http_build_query($getData));
34
        }
35
36
        return self::responseHandler(
37
            'GET',
38
            $requestPath,
39
            $this->httpClient->get($requestUri)
40
        );
41
    }
42
43
    public function post($requestPath, array $postData)
44
    {
45
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);
46
47
        return self::responseHandler(
48
            'POST',
49
            $requestPath,
50
            $this->httpClient->post($requestUri, $postData)
51
        );
52
    }
53
54
    private static function responseHandler($requestMethod, $requestPath, array $clientResponse)
55
    {
56
        list($statusCode, $responseData) = $clientResponse;
57
        self::validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData);
58
59
        if (400 <= $statusCode) {
60
            // either we sent an incorrect request, or there is a server error
61
            throw new HttpClientException(sprintf('[%d] %s "/%s": %s', $statusCode, $requestMethod, $requestPath, $responseData['error']));
62
        }
63
64
        // the request was correct, and there was not a server error
65
        if ($responseData[$requestPath]['ok']) {
66
            // our request was handled correctly
67
            if (array_key_exists('data', $responseData[$requestPath])) {
68
                return $responseData[$requestPath]['data'];
69
            }
70
71
            return true;
72
        }
73
74
        // our request was not handled correctly, something went wrong...
75
        throw new ApiException($responseData[$requestPath]['error']);
76
    }
77
78
    private static function validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData)
79
    {
80
        // responseData MUST be array
81
        if (!is_array($responseData)) {
82
            throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST be array', $statusCode, $requestMethod, $requestPath));
83
        }
84
85 View Code Duplication
        if (400 <= $statusCode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
            // if status code is 4xx or 5xx it MUST have an 'error' field
87
            if (!array_key_exists('error', $responseData)) {
88
                throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "error" field', $statusCode, $requestMethod, $requestPath));
89
            }
90
91
            return;
92
        }
93
94 View Code Duplication
        if (!array_key_exists($requestPath, $responseData)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s" field', $statusCode, $requestMethod, $requestPath, $requestPath));
96
        }
97
98 View Code Duplication
        if (!array_key_exists('ok', $responseData[$requestPath])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
            throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s/ok" field', $statusCode, $requestMethod, $requestPath, $requestPath));
100
        }
101
102
        if (!$responseData[$requestPath]['ok']) {
103
            // not OK response, MUST contain error field
104 View Code Duplication
            if (!array_key_exists('error', $responseData[$requestPath])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
                throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s/error" field', $statusCode, $requestMethod, $requestPath, $requestPath));
106
            }
107
        }
108
    }
109
}
110