1
|
|
|
<?php |
2
|
|
|
namespace Pbxg33k\MusicInfo; |
3
|
|
|
|
4
|
|
|
use Pbxg33k\MusicInfo\Exception\ServiceConfigurationException; |
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Pbxg33k\MusicInfo\Model\IMusicService; |
9
|
|
|
use Pbxg33k\MusicInfo\Service\BaseService; |
10
|
|
|
use Symfony\Component\Config\FileLocator; |
11
|
|
|
|
12
|
|
|
class MusicInfo |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ClientInterface |
16
|
|
|
*/ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $services; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Supported Services |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $supportedServices = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* MusicInfo constructor. |
37
|
|
|
* @param $config |
38
|
|
|
* @throws ServiceConfigurationException if musicinfo.service is missing |
39
|
|
|
*/ |
40
|
10 |
|
public function __construct($config) |
41
|
|
|
{ |
42
|
10 |
|
$this->config = $config; |
43
|
|
|
|
44
|
10 |
|
$this->services = new ArrayCollection(); |
|
|
|
|
45
|
10 |
|
$this->setClient( |
46
|
10 |
|
new Client($config['defaults']['guzzle']) |
47
|
10 |
|
); |
48
|
|
|
|
49
|
10 |
|
if(isset($config['services'])) { |
50
|
10 |
|
foreach($config['services'] as $service) { |
51
|
10 |
|
if(!isset($config['init_services'])) { |
52
|
1 |
|
$config['init_services'] = null; |
53
|
1 |
|
} |
54
|
10 |
|
$this->loadService($service, $config['init_services']); |
55
|
10 |
|
$this->supportedServices[] = $service; |
56
|
10 |
|
} |
57
|
10 |
|
} else { |
58
|
1 |
|
throw new ServiceConfigurationException("musicinfo.services is required"); |
59
|
|
|
} |
60
|
|
|
|
61
|
10 |
|
return $this->getServices(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $configDirectory |
66
|
|
|
*/ |
67
|
|
|
public function loadConfig($configDirectory) |
68
|
|
|
{ |
69
|
|
|
$locator = new FileLocator($configDirectory); |
70
|
|
|
$generalConfig = $locator->locate('config.yml'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ClientInterface $client |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
10 |
|
public function setClient(ClientInterface $client) |
79
|
|
|
{ |
80
|
10 |
|
$this->client = $client; |
81
|
|
|
|
82
|
10 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return ClientInterface |
87
|
|
|
*/ |
88
|
10 |
|
public function getClient() |
89
|
|
|
{ |
90
|
10 |
|
return $this->client; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Load service |
95
|
|
|
* |
96
|
|
|
* @param $service |
97
|
|
|
* @param $init |
98
|
|
|
* |
99
|
|
|
* @return IMusicService |
100
|
|
|
* |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
10 |
|
public function loadService($service, $init = false) |
104
|
|
|
{ |
105
|
10 |
|
$fqcn = implode('\\',['Pbxg33k', 'MusicInfo', 'Service', $service, 'Service']); |
106
|
10 |
|
if(class_exists($fqcn)) { |
107
|
|
|
/** @var IMusicService $client */ |
108
|
10 |
|
$client = new $fqcn(); |
109
|
10 |
|
$client->setConfig($this->mergeConfig($service)); |
110
|
10 |
|
$client->setClient($this->getClient()); |
111
|
10 |
|
if($init == true) { |
|
|
|
|
112
|
1 |
|
$client->init(); |
113
|
1 |
|
} |
114
|
10 |
|
$this->addService($client, $service); |
115
|
10 |
|
return $service; |
116
|
|
|
} else { |
117
|
1 |
|
throw new \Exception('Service class does not exist: '.$service.' ('.$fqcn.')'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Merge shared config with service specific configuration |
123
|
|
|
* |
124
|
|
|
* @param $service |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
10 |
|
public function mergeConfig($service) |
128
|
|
|
{ |
129
|
10 |
|
$service = strtolower($service); |
130
|
10 |
|
if(isset($this->config[$service])) { |
131
|
10 |
|
$config = array_merge( |
132
|
10 |
|
$this->config['defaults'], |
133
|
10 |
|
$this->config[$service] |
134
|
10 |
|
); |
135
|
10 |
|
return $config; |
136
|
|
|
} else { |
137
|
|
|
return $this->config['defaults']; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Load all services |
143
|
|
|
* |
144
|
|
|
* @return bool|ArrayCollection |
145
|
|
|
*/ |
146
|
1 |
|
public function loadServices() |
147
|
|
|
{ |
148
|
1 |
|
foreach($this->supportedServices as $service) { |
149
|
1 |
|
$this->loadService($service); |
150
|
1 |
|
} |
151
|
|
|
|
152
|
1 |
|
return $this->getServices(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param IMusicService $service |
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
10 |
|
public function addService(IMusicService $service, $key) |
161
|
|
|
{ |
162
|
10 |
|
$this->services[strtolower($key)] = $service; |
163
|
10 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return ArrayCollection |
168
|
|
|
*/ |
169
|
10 |
|
public function getServices() |
170
|
|
|
{ |
171
|
10 |
|
return $this->services; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param $key |
176
|
|
|
* @return IMusicService|null |
177
|
|
|
*/ |
178
|
4 |
|
public function getService($key) |
179
|
|
|
{ |
180
|
4 |
|
$key = strtolower($key); |
181
|
4 |
|
if(isset($this->services[$key])) { |
182
|
3 |
|
return $this->initializeService($this->services[$key]); |
183
|
|
|
} else { |
184
|
2 |
|
return null; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param BaseService $service |
190
|
|
|
* |
191
|
|
|
* @return BaseService |
192
|
|
|
* @throws ServiceConfigurationException |
193
|
|
|
*/ |
194
|
3 |
|
public function initializeService(BaseService $service) |
195
|
|
|
{ |
196
|
3 |
|
if(!$service->isInitialized()) { |
197
|
3 |
|
$service->init(); |
198
|
3 |
|
} |
199
|
|
|
|
200
|
3 |
|
return $service; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $key |
205
|
|
|
* |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
1 |
|
public function removeService($key) |
209
|
|
|
{ |
210
|
1 |
|
if(isset($this->services[$key])) { |
211
|
1 |
|
unset($this->services[$key]); |
212
|
1 |
|
} |
213
|
|
|
|
214
|
1 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return IMusicService |
219
|
|
|
*/ |
220
|
|
|
public function getPreferredService() |
221
|
|
|
{ |
222
|
|
|
return $this->getService($this->config['preferred_order'][0]); |
223
|
|
|
} |
224
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..