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