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\LastFm\Connection; |
13
|
|
|
|
14
|
|
|
use Core23\LastFm\Exception\ApiException; |
15
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
16
|
|
|
use Psr\Http\Client\ClientInterface; |
17
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
|
21
|
|
|
final class PsrClientConnection implements ConnectionInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ClientInterface |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RequestFactoryInterface |
30
|
|
|
*/ |
31
|
|
|
private $requestFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $endpoint; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initialize client. |
40
|
|
|
*/ |
41
|
|
|
public function __construct(ClientInterface $client, RequestFactoryInterface $messageFactory, string $endpoint = ConnectionInterface::DEFAULT_ENDPOINT) |
42
|
|
|
{ |
43
|
|
|
$this->client = $client; |
44
|
|
|
$this->requestFactory = $messageFactory; |
45
|
|
|
$this->endpoint = $endpoint; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getPageBody(string $url, array $params = [], string $method = 'GET'): ?string |
49
|
|
|
{ |
50
|
|
|
$request = $this->createRequest($method, $url, $params); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$response = $this->client->sendRequest($request); |
54
|
|
|
} catch (ClientExceptionInterface $e) { |
55
|
|
|
throw new ApiException( |
56
|
|
|
sprintf('Error fetching page body for url: %s', (string) $request->getUri()), |
57
|
|
|
500, |
58
|
|
|
$e |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($response->getStatusCode() >= 400) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $response->getBody()->getContents(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function call(string $url, array $params = [], string $method = 'GET'): array |
70
|
|
|
{ |
71
|
|
|
$params = array_merge($params, ['format' => 'json']); |
72
|
|
|
$request = $this->createRequest($method, $this->endpoint, $params); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$response = $this->client->sendRequest($request); |
76
|
|
|
|
77
|
|
|
// Parse response |
78
|
|
|
return $this->parseResponse($response); |
79
|
|
|
} catch (ApiException $e) { |
80
|
|
|
throw $e; |
81
|
|
|
} catch (\Exception $e) { |
82
|
|
|
throw new ApiException('Technical error occurred.', 500, $e); |
83
|
|
|
} catch (ClientExceptionInterface $e) { |
84
|
|
|
throw new ApiException('Technical error occurred.', 500, $e); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function createRequest(string $method, string $url, array $params): RequestInterface |
89
|
|
|
{ |
90
|
|
|
$query = http_build_query($params); |
91
|
|
|
|
92
|
|
|
if ('POST' === $method) { |
93
|
|
|
$request = $this->requestFactory->createRequest($method, $url); |
94
|
|
|
$request->getBody()->write($query); |
95
|
|
|
|
96
|
|
|
return $request; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->requestFactory->createRequest($method, $url.'?'.$query); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @throws ApiException |
104
|
|
|
*/ |
105
|
|
|
private function parseResponse(ResponseInterface $response): array |
106
|
|
|
{ |
107
|
|
|
$array = json_decode($response->getBody()->getContents(), true); |
108
|
|
|
|
109
|
|
|
if (\is_array($array) && \array_key_exists('error', $array) && \array_key_exists('message', $array)) { |
110
|
|
|
throw new ApiException($array['message'], $array['error']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $array; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|