|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Christian Gripp <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Core23\LastFm\Client; |
|
11
|
|
|
|
|
12
|
|
|
use Core23\LastFm\Connection\ConnectionInterface; |
|
13
|
|
|
use Core23\LastFm\Exception\ApiException; |
|
14
|
|
|
use Core23\LastFm\Exception\NotFoundException; |
|
15
|
|
|
use Core23\LastFm\Session\SessionInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class ApiClient implements ApiClientInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConnectionInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $connection; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $apiKey; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $sharedSecret; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ConnectionInterface $connection |
|
36
|
|
|
* @param string $apiKey |
|
37
|
|
|
* @param string $sharedSecret |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(ConnectionInterface $connection, string $apiKey, string $sharedSecret) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->connection = $connection; |
|
42
|
|
|
$this->apiKey = $apiKey; |
|
43
|
|
|
$this->sharedSecret = $sharedSecret; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getApiKey(): string |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->apiKey; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getSharedSecret(): string |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->sharedSecret; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function signedCall(string $method, array $params = [], SessionInterface $session = null, $requestMethod = 'GET'): array |
|
66
|
|
|
{ |
|
67
|
|
|
// Call parameter |
|
68
|
|
|
$callParams = [ |
|
69
|
|
|
'method' => $method, |
|
70
|
|
|
'api_key' => $this->apiKey, |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
// Add session key |
|
74
|
|
|
if (null !== $session) { |
|
75
|
|
|
$callParams['sk'] = $session->getKey(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$params = array_merge($callParams, $params); |
|
79
|
|
|
$params = $this->filterNull($params); |
|
80
|
|
|
$params = $this->encodeUTF8($params); |
|
81
|
|
|
|
|
82
|
|
|
// Sign parameter |
|
83
|
|
|
$params['api_sig'] = $this->signParams($params); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
return $this->call($method, $params); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function unsignedCall(string $method, array $params = [], string $requestMethod = 'GET'): array |
|
92
|
|
|
{ |
|
93
|
|
|
// Call parameter |
|
94
|
|
|
$callParameter = [ |
|
95
|
|
|
'method' => $method, |
|
96
|
|
|
'api_key' => $this->apiKey, |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
$params = array_merge($callParameter, $params); |
|
100
|
|
|
$params = $this->filterNull($params); |
|
101
|
|
|
$params = $this->encodeUTF8($params); |
|
102
|
|
|
|
|
103
|
|
|
return $this->call($method, $params); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Filter null values. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $object |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
private function filterNull(array $object): array |
|
114
|
|
|
{ |
|
115
|
|
|
return array_filter($object, static function ($val) { |
|
116
|
|
|
return null !== $val; |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Converts any string or array of strings to UTF8. |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed|mixed[] $object a String or an array |
|
124
|
|
|
* |
|
125
|
|
|
* @return mixed|mixed[] UTF8-string or array |
|
126
|
|
|
*/ |
|
127
|
|
|
private function encodeUTF8($object) |
|
128
|
|
|
{ |
|
129
|
|
|
if (\is_array($object)) { |
|
130
|
|
|
return array_map([$this, 'encodeUTF8'], $object); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return mb_convert_encoding((string) $object, 'UTF-8', 'auto'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param array $params |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
private function signParams(array $params): string |
|
142
|
|
|
{ |
|
143
|
|
|
ksort($params); |
|
144
|
|
|
|
|
145
|
|
|
$signature = ''; |
|
146
|
|
|
foreach ($params as $name => $value) { |
|
147
|
|
|
$signature .= $name.$value; |
|
148
|
|
|
} |
|
149
|
|
|
$signature .= $this->sharedSecret; |
|
150
|
|
|
|
|
151
|
|
|
return md5($signature); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param string $method |
|
156
|
|
|
* @param array $params |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
private function call(string $method, array $params): array |
|
161
|
|
|
{ |
|
162
|
|
|
try { |
|
163
|
|
|
return $this->connection->call($method, $params); |
|
164
|
|
|
} catch (ApiException $e) { |
|
165
|
|
|
if (6 === (int) $e->getCode()) { |
|
166
|
|
|
throw new NotFoundException('No entity was found for your request.', $e->getCode(), $e); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
throw $e; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.