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\Service; |
13
|
|
|
|
14
|
|
|
use Core23\LastFm\Connection\ConnectionInterface; |
15
|
|
|
use Core23\LastFm\Exception\ApiException; |
16
|
|
|
use Core23\LastFm\Exception\NotFoundException; |
17
|
|
|
use Core23\LastFm\Session\SessionInterface; |
18
|
|
|
|
19
|
|
|
abstract class AbstractService |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ConnectionInterface |
23
|
|
|
*/ |
24
|
|
|
private $connection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ConnectionInterface $connection |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ConnectionInterface $connection) |
30
|
|
|
{ |
31
|
|
|
$this->connection = $connection; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return ConnectionInterface |
36
|
|
|
*/ |
37
|
|
|
protected function getConnection(): ConnectionInterface |
38
|
|
|
{ |
39
|
|
|
return $this->connection; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Calls the API with signed session. |
44
|
|
|
* |
45
|
|
|
* @param string $method |
46
|
|
|
* @param array $params |
47
|
|
|
* @param SessionInterface|null $session |
48
|
|
|
* @param string $requestMethod |
49
|
|
|
* |
50
|
|
|
* @throws ApiException |
51
|
|
|
* @throws NotFoundException |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
final protected function signedCall(string $method, array $params = [], SessionInterface $session = null, $requestMethod = 'GET'): array |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
|
|
return $this->connection->signedCall($method, $params, $session, $requestMethod); |
59
|
|
|
} catch (ApiException $e) { |
60
|
|
|
if (6 === (int) $e->getCode()) { |
61
|
|
|
throw new NotFoundException('No entity was found for your request.', $e->getCode(), $e); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw $e; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Calls the API unsigned. |
70
|
|
|
* |
71
|
|
|
* @param string $method |
72
|
|
|
* @param array $params |
73
|
|
|
* @param string $requestMethod |
74
|
|
|
* |
75
|
|
|
* @throws ApiException |
76
|
|
|
* @throws NotFoundException |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
final protected function unsignedCall(string $method, array $params = [], $requestMethod = 'GET'): array |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
return $this->connection->unsignedCall($method, $params, $requestMethod); |
84
|
|
|
} catch (ApiException $e) { |
85
|
|
|
if (6 === (int) $e->getCode()) { |
86
|
|
|
throw new NotFoundException('No entity was found for your request.', $e->getCode(), $e); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw $e; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|