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 Http\Client\Exception as ClientException; |
18
|
|
|
use Http\Client\HttpClient; |
19
|
|
|
use Http\Message\RequestFactory; |
20
|
|
|
use Psr\Http\Message\RequestInterface; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
|
23
|
|
|
final class HTTPlugConnection extends AbstractConnection |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var HttpClient |
27
|
|
|
*/ |
28
|
|
|
private $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RequestFactory |
32
|
|
|
*/ |
33
|
|
|
private $requestFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initialize client. |
37
|
|
|
* |
38
|
|
|
* @param HttpClient $client |
39
|
|
|
* @param RequestFactory $requestFactory |
40
|
|
|
* @param string $apiKey |
41
|
|
|
* @param string $uri |
42
|
|
|
*/ |
43
|
|
|
public function __construct(HttpClient $client, RequestFactory $requestFactory, string $apiKey, string $uri = null) |
44
|
|
|
{ |
45
|
|
|
parent::__construct($apiKey, $uri); |
46
|
|
|
|
47
|
|
|
$this->client = $client; |
48
|
|
|
$this->requestFactory = $requestFactory; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function call(string $method, array $params = [], string $requestMethod = 'GET'): array |
55
|
|
|
{ |
56
|
|
|
$request = $this->buildRequest($method, $params, $requestMethod); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$response = $this->client->sendRequest($request); |
60
|
|
|
|
61
|
|
|
return $this->parseResponse($response); |
62
|
|
|
} catch (ApiException | NotFoundException $e) { |
63
|
|
|
throw $e; |
64
|
|
|
} catch (Exception $e) { |
65
|
|
|
throw new ApiException('Technical error occurred.', 500, $e); |
66
|
|
|
} catch (ClientException $e) { |
67
|
|
|
throw new ApiException('Technical error occurred.', $e->getCode(), $e); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param ResponseInterface $response |
73
|
|
|
* |
74
|
|
|
* @throws ApiException |
75
|
|
|
* @throws NotFoundException |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
private function parseResponse(ResponseInterface $response): array |
80
|
|
|
{ |
81
|
|
|
$content = $response->getBody()->getContents(); |
82
|
|
|
$array = json_decode($content, true); |
83
|
|
|
|
84
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
85
|
|
|
throw new ApiException('Server did not reply with a valid response.', $response->getStatusCode()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (404 === $response->getStatusCode()) { |
89
|
|
|
throw new NotFoundException('Server did not find any entity for the request.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($response->getStatusCode() >= 400) { |
93
|
|
|
throw new ApiException('Technical error occurred.', $response->getStatusCode()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $array; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Builds request parameter. |
101
|
|
|
* |
102
|
|
|
* @param array $parameter |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
private static function buildParameter(array $parameter): string |
107
|
|
|
{ |
108
|
|
|
return http_build_query($parameter); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $method |
113
|
|
|
* @param array $params |
114
|
|
|
* @param string $requestMethod |
115
|
|
|
* |
116
|
|
|
* @return RequestInterface |
117
|
|
|
*/ |
118
|
|
|
private function buildRequest(string $method, array $params, string $requestMethod): RequestInterface |
119
|
|
|
{ |
120
|
|
|
$data = self::buildParameter($params); |
121
|
|
|
|
122
|
|
|
$headers = [ |
123
|
|
|
'Accept' => 'application/json', |
124
|
|
|
'x-api-key' => $this->getApiKey(), |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
if ('POST' === $requestMethod) { |
128
|
|
|
return $this->requestFactory->createRequest($requestMethod, $this->getUri().$method, $headers, $data); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->requestFactory->createRequest($requestMethod, $this->getUri().$method.'?'.$data, $headers); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|