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 |
|
|
|
|
109
|
|
|
->withHeader('Accept', 'application/json') |
110
|
|
|
->withHeader('x-api-key', $this->getApiKey()) |
111
|
|
|
; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: