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 ( 7655d7...20ce7a )
by François
02:01
created

ServerClient::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Common\HttpClient;
20
21
use SURFnet\VPN\Common\HttpClient\Exception\ApiException;
22
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException;
23
24
class ServerClient
25
{
26
    /** @var HttpClientInterface */
27
    private $httpClient;
28
29
    /** @var string */
30
    private $baseUri;
31
32
    public function __construct(HttpClientInterface $httpClient, $baseUri)
33
    {
34
        $this->httpClient = $httpClient;
35
        $this->baseUri = $baseUri;
36
    }
37
38
    public function get($requestPath, array $getData = [])
39
    {
40
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);
41
        if (0 !== count($getData)) {
42
            $requestUri = sprintf('%s?%s', $requestUri, http_build_query($getData));
43
        }
44
45
        return self::responseHandler(
46
            'GET',
47
            $requestPath,
48
            $this->httpClient->get($requestUri)
49
        );
50
    }
51
52
    public function post($requestPath, array $postData)
53
    {
54
        $requestUri = sprintf('%s/%s', $this->baseUri, $requestPath);
55
56
        return self::responseHandler(
57
            'POST',
58
            $requestPath,
59
            $this->httpClient->post($requestUri, $postData)
60
        );
61
    }
62
63
    private static function responseHandler($requestMethod, $requestPath, array $clientResponse)
64
    {
65
        list($statusCode, $responseData) = $clientResponse;
66
        self::validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData);
67
68
        if (400 <= $statusCode) {
69
            // either we sent an incorrect request, or there is a server error
70
            throw new HttpClientException(sprintf('[%d] %s "/%s": %s', $statusCode, $requestMethod, $requestPath, $responseData['error']));
71
        }
72
73
        // the request was correct, and there was not a server error
74
        if ($responseData[$requestPath]['ok']) {
75
            // our request was handled correctly
76
            if (array_key_exists('data', $responseData[$requestPath])) {
77
                return $responseData[$requestPath]['data'];
78
            }
79
80
            return true;
81
        }
82
83
        // our request was not handled correctly, something went wrong...
84
        throw new ApiException($responseData[$requestPath]['error']);
85
    }
86
87
    private static function validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData)
88
    {
89
        // responseData MUST be array
90
        if (!is_array($responseData)) {
91
            throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST be array', $statusCode, $requestMethod, $requestPath));
92
        }
93
94 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...
95
            // if status code is 4xx or 5xx it MUST have an 'error' field
96
            if (!array_key_exists('error', $responseData)) {
97
                throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "error" field', $statusCode, $requestMethod, $requestPath));
98
            }
99
100
            return;
101
        }
102
103 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...
104
            throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s" field', $statusCode, $requestMethod, $requestPath, $requestPath));
105
        }
106
107 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...
108
            throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s/ok" field', $statusCode, $requestMethod, $requestPath, $requestPath));
109
        }
110
111
        if (!$responseData[$requestPath]['ok']) {
112
            // not OK response, MUST contain error field
113 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...
114
                throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s/error" field', $statusCode, $requestMethod, $requestPath, $requestPath));
115
            }
116
        }
117
    }
118
}
119