|
1
|
|
|
<?php |
|
2
|
|
|
namespace Sourceout\LastFm; |
|
3
|
|
|
|
|
4
|
|
|
use Sourceout\LastFm\Http\Http; |
|
5
|
|
|
use Sourceout\LastFm\Http\HttpInterface; |
|
6
|
|
|
use Sourceout\LastFm\Providers\GeoInterface; |
|
7
|
|
|
use Sourceout\LastFm\Services\ServiceFactory; |
|
8
|
|
|
use Sourceout\LastFm\Providers\ProviderInterface; |
|
9
|
|
|
use Sourceout\LastFm\Providers\ResourceInterface; |
|
10
|
|
|
use Sourceout\LastFm\Exception\ProviderDoesNotExistException; |
|
11
|
|
|
use Sourceout\LastFm\Exception\UnregisteredProviderException; |
|
12
|
|
|
use Sourceout\LastFm\Exception\IncomptabileProviderTypeException; |
|
13
|
|
|
|
|
14
|
|
|
class Client |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string[] a list of default providers */ |
|
17
|
|
|
private $providers = [ |
|
18
|
|
|
\Sourceout\LastFm\Providers\LastFm\LastFm::class |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array a list of provider interfaces */ |
|
22
|
|
|
private $providerInterfaces = [ |
|
23
|
|
|
\Sourceout\LastFm\Providers\ProviderInterface::class, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** @var HttpInterface|null an instance of http used to fetch data */ |
|
27
|
|
|
private $http; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
7 |
|
public function __construct(HttpInterface $http = null) |
|
31
|
|
|
{ |
|
32
|
7 |
|
$this->http = $http; |
|
33
|
7 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a list of registered providers |
|
37
|
|
|
* |
|
38
|
|
|
* @return string[] |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function getRegisteredProviders() : array |
|
41
|
|
|
{ |
|
42
|
4 |
|
return $this->providers; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Add ability to add new custom providers |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $providers |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
* @throws ProviderDoesNotExistsException |
|
52
|
|
|
* @throws IncomptabileProviderTypeException |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function registerCustomProviders(array $providers) : void |
|
55
|
|
|
{ |
|
56
|
3 |
|
foreach ($providers as $provider) { |
|
57
|
3 |
|
if (!class_exists($provider)) { |
|
58
|
1 |
|
throw new ProviderDoesNotExistException( |
|
59
|
1 |
|
"Provider {$provider} does not exists" |
|
60
|
|
|
); |
|
61
|
|
|
} else if ( |
|
62
|
2 |
|
!(array_intersect( |
|
63
|
2 |
|
$this->providerInterfaces, |
|
64
|
2 |
|
class_implements($provider) |
|
65
|
2 |
|
) == $this->providerInterfaces |
|
66
|
|
|
) |
|
67
|
|
|
) { |
|
68
|
1 |
|
throw new IncomptabileProviderTypeException( |
|
69
|
1 |
|
"Provider {$provider} is not compatible" |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
1 |
|
array_push($this->providers, $provider); |
|
73
|
|
|
} |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns an instance of ServiceFactory |
|
78
|
|
|
* |
|
79
|
|
|
* @param ProviderInterface $provider |
|
80
|
|
|
* |
|
81
|
|
|
* @return ServiceFactory |
|
82
|
|
|
* @throws UnregisteredProviderException |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function getServiceFactory( |
|
85
|
|
|
ProviderInterface $provider |
|
86
|
|
|
) : ServiceFactory |
|
87
|
|
|
{ |
|
|
|
|
|
|
88
|
2 |
|
$class = get_class($provider); |
|
89
|
2 |
|
if (!in_array($class, $this->getRegisteredProviders())) { |
|
90
|
1 |
|
throw new UnregisteredProviderException( |
|
91
|
1 |
|
"Provider {$class} is not registered" |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
$http = $this->http ?: (new Http()); |
|
96
|
1 |
|
return new ServiceFactory($provider, $http); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Setter Method |
|
101
|
|
|
* |
|
102
|
|
|
* @param HttpInterface $http |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function setHttpClient(HttpInterface $http) : void |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->http = $http; |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Getter Method |
|
112
|
|
|
* |
|
113
|
|
|
* @return HttpInterface|null |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function getHttpClient() : ?HttpInterface |
|
116
|
|
|
{ |
|
117
|
1 |
|
return $this->http; |
|
118
|
|
|
} |
|
119
|
|
|
} |