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
Pull Request — master (#26)
by Christian
07:20
created

PsrClientConnection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 0
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A call() 0 16 4
A parseResponse() 0 19 4
A buildParameter() 0 4 1
A buildRequest() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\SetlistFm\Connection;
13
14
use Core23\SetlistFm\Exception\ApiException;
15
use Core23\SetlistFm\Exception\NotFoundException;
16
use Exception;
17
use Psr\Http\Client\ClientExceptionInterface;
18
use Psr\Http\Client\ClientInterface;
19
use Psr\Http\Message\RequestFactoryInterface;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
23
final class PsrClientConnection extends AbstractConnection
24
{
25
    /**
26
     * @var ClientInterface
27
     */
28
    private $client;
29
30
    /**
31
     * @var RequestFactoryInterface
32
     */
33
    private $requestFactory;
34
35
    /**
36
     * Initialize client.
37
     *
38
     * @param string $uri
39
     */
40
    public function __construct(ClientInterface $client, RequestFactoryInterface $requestFactory, string $apiKey, string $uri = null)
41
    {
42
        parent::__construct($apiKey, $uri);
43
44
        $this->client         = $client;
45
        $this->requestFactory = $requestFactory;
46
    }
47
48
    public function call(string $method, array $params = [], string $requestMethod = 'GET'): array
49
    {
50
        $request = $this->buildRequest($method, $params, $requestMethod);
51
52
        try {
53
            $response = $this->client->sendRequest($request);
54
55
            return $this->parseResponse($response);
56
        } catch (ApiException | NotFoundException $e) {
57
            throw $e;
58
        } catch (Exception $e) {
59
            throw new ApiException('Technical error occurred.', 500, $e);
60
        } catch (ClientExceptionInterface $e) {
61
            throw new ApiException('Technical error occurred.', 500, $e);
62
        }
63
    }
64
65
    /**
66
     * @throws ApiException
67
     * @throws NotFoundException
68
     */
69
    private function parseResponse(ResponseInterface $response): array
70
    {
71
        $content = $response->getBody()->getContents();
72
        $array   = json_decode($content, true);
73
74
        if (JSON_ERROR_NONE !== json_last_error()) {
75
            throw new ApiException('Server did not reply with a valid response.', $response->getStatusCode());
76
        }
77
78
        if (404 === $response->getStatusCode()) {
79
            throw new NotFoundException('Server did not find any entity for the request.');
80
        }
81
82
        if ($response->getStatusCode() >= 400) {
83
            throw new ApiException('Technical error occurred.', $response->getStatusCode());
84
        }
85
86
        return $array;
87
    }
88
89
    /**
90
     * Builds request parameter.
91
     */
92
    private static function buildParameter(array $parameter): string
93
    {
94
        return http_build_query($parameter);
95
    }
96
97
    private function buildRequest(string $method, array $params, string $requestMethod): RequestInterface
98
    {
99
        $data = self::buildParameter($params);
100
101
        if ('POST' === $requestMethod) {
102
            $request =  $this->requestFactory->createRequest($requestMethod, $this->getUri().$method);
103
            $request->getBody()->write($data);
104
        } else {
105
            $this->requestFactory->createRequest($requestMethod, $this->getUri().$method.'?'.$data);
106
        }
107
108
        return $request
0 ignored issues
show
Bug introduced by
The variable $request does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
109
            ->withHeader('Accept', 'application/json')
110
            ->withHeader('x-api-key', $this->getApiKey())
111
        ;
112
    }
113
}
114