1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the Pbxg33k\MusicInfo package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* (c) 2017 Oguzhan uysal. All rights reserved |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Pbxg33k\MusicInfo; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\ClientInterface; |
16
|
|
|
use Pbxg33k\MusicInfo\Exception\ServiceConfigurationException; |
17
|
|
|
use Pbxg33k\MusicInfo\Model\IMusicService; |
18
|
|
|
use Pbxg33k\MusicInfo\Service\BaseService; |
19
|
|
|
use Pbxg33k\Traits\PropertyTrait; |
20
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
21
|
|
|
use Symfony\Component\Cache\Adapter\NullAdapter; |
22
|
|
|
|
23
|
|
|
class MusicInfo |
24
|
|
|
{ |
25
|
|
|
use PropertyTrait; |
26
|
|
|
/** |
27
|
|
|
* @var ClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ArrayCollection |
33
|
|
|
*/ |
34
|
|
|
protected $services; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CacheItemPoolInterface |
43
|
|
|
*/ |
44
|
|
|
protected $cache; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Supported Services |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $supportedServices = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* MusicInfo constructor. |
55
|
|
|
* |
56
|
|
|
* @param $config |
57
|
|
|
* |
58
|
|
|
* @throws ServiceConfigurationException if musicinfo.service is missing |
59
|
|
|
*/ |
60
|
39 |
|
public function __construct($config) |
61
|
|
|
{ |
62
|
39 |
|
$this->config = $config; |
63
|
|
|
|
64
|
|
|
// Set NullAdapter for cache to prevent exceptions |
65
|
39 |
|
$this->setCache(new NullAdapter()); |
66
|
|
|
|
67
|
39 |
|
$this->services = new ArrayCollection(); |
68
|
39 |
|
$this->setClient( |
69
|
39 |
|
new Client($config['defaults']['guzzle']) |
70
|
39 |
|
); |
71
|
|
|
|
72
|
39 |
|
if (isset($config['services'])) { |
73
|
39 |
|
foreach ($config['services'] as $service) { |
74
|
39 |
|
if (!isset($config['init_services'])) { |
75
|
1 |
|
$config['init_services'] = null; |
76
|
1 |
|
} |
77
|
39 |
|
$this->loadService($service, $config['init_services']); |
78
|
39 |
|
$this->supportedServices[] = $service; |
79
|
39 |
|
} |
80
|
39 |
|
} else { |
81
|
1 |
|
throw new ServiceConfigurationException("musicinfo.services is required"); |
82
|
|
|
} |
83
|
|
|
|
84
|
39 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param ClientInterface $client |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
39 |
|
public function setClient(ClientInterface $client) |
92
|
|
|
{ |
93
|
39 |
|
$this->client = $client; |
94
|
|
|
|
95
|
39 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return ClientInterface |
100
|
|
|
*/ |
101
|
39 |
|
public function getClient() |
102
|
|
|
{ |
103
|
39 |
|
return $this->client; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
39 |
|
public function setCache(CacheItemPoolInterface $cacheItemPool) |
111
|
|
|
{ |
112
|
39 |
|
$this->cache = $cacheItemPool; |
113
|
|
|
|
114
|
39 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return CacheItemPoolInterface |
119
|
|
|
*/ |
120
|
39 |
|
public function getCache() |
121
|
|
|
{ |
122
|
39 |
|
return $this->cache; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Load service |
127
|
|
|
* |
128
|
|
|
* @param $service |
129
|
|
|
* @param $init |
130
|
|
|
* |
131
|
|
|
* @return IMusicService |
132
|
|
|
* |
133
|
|
|
* @throws \Exception |
134
|
|
|
*/ |
135
|
39 |
|
public function loadService($service, $init = false) |
136
|
|
|
{ |
137
|
39 |
|
$fqcn = implode('\\', ['Pbxg33k', 'MusicInfo', 'Service', $service, 'Service']); |
138
|
39 |
|
if (class_exists($fqcn)) { |
139
|
|
|
/** |
140
|
|
|
* |
141
|
|
|
* |
142
|
|
|
* @var IMusicService $client |
143
|
|
|
*/ |
144
|
39 |
|
$client = new $fqcn(); |
145
|
39 |
|
$client->setConfig($this->mergeConfig($service)); |
146
|
39 |
|
$client->setClient($this->getClient()); |
147
|
39 |
|
$client->setCache($this->getCache()); |
148
|
39 |
|
if ($init === true) { |
149
|
39 |
|
$client->init(); |
150
|
39 |
|
} |
151
|
39 |
|
$this->addService($client, $service); |
152
|
|
|
|
153
|
39 |
|
return $service; |
154
|
|
|
} else { |
155
|
1 |
|
throw new \Exception('Service class does not exist: ' . $service . ' (' . $fqcn . ')'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Merge shared config with service specific configuration |
161
|
|
|
* |
162
|
|
|
* @param $service |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
39 |
|
public function mergeConfig($service) |
167
|
|
|
{ |
168
|
39 |
|
$service = strtolower($service); |
169
|
39 |
|
if (isset($this->config['service_configuration'][$service])) { |
170
|
39 |
|
$config = array_merge( |
171
|
39 |
|
$this->config['defaults'], |
172
|
39 |
|
$this->config['service_configuration'][$service] |
173
|
39 |
|
); |
174
|
|
|
|
175
|
39 |
|
return $config; |
176
|
|
|
} else { |
177
|
7 |
|
return $this->config['defaults']; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Load all services |
183
|
|
|
* |
184
|
|
|
* @param bool $initialize |
185
|
|
|
* |
186
|
|
|
* @return ArrayCollection |
187
|
|
|
* @throws \Exception |
188
|
|
|
*/ |
189
|
5 |
|
public function loadServices($initialize = false) |
190
|
|
|
{ |
191
|
5 |
|
foreach ($this->supportedServices as $service) { |
192
|
5 |
|
$this->loadService($service, $initialize); |
193
|
5 |
|
} |
194
|
|
|
|
195
|
5 |
|
return $this->getServices(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param IMusicService $service |
200
|
|
|
* @param $key |
201
|
|
|
* |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
39 |
|
public function addService(IMusicService $service, $key) |
205
|
|
|
{ |
206
|
39 |
|
$this->services[strtolower($key)] = $service; |
207
|
|
|
|
208
|
39 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return ArrayCollection |
213
|
|
|
*/ |
214
|
6 |
|
public function getServices() |
215
|
|
|
{ |
216
|
6 |
|
return $this->services; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param $key |
221
|
|
|
* |
222
|
|
|
* @return BaseService|null |
223
|
|
|
*/ |
224
|
24 |
|
public function getService($key) |
225
|
|
|
{ |
226
|
24 |
|
$key = strtolower($key); |
227
|
24 |
|
if (isset($this->services[$key])) { |
228
|
22 |
|
return $this->initializeService($this->services[$key]); |
229
|
|
|
} else { |
230
|
3 |
|
return null; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param BaseService $service |
236
|
|
|
* |
237
|
|
|
* @return BaseService |
238
|
|
|
* @throws ServiceConfigurationException |
239
|
|
|
*/ |
240
|
22 |
|
public function initializeService(BaseService $service) |
241
|
|
|
{ |
242
|
22 |
|
if (!$service->isInitialized()) { |
243
|
1 |
|
$service->init(); |
244
|
1 |
|
} |
245
|
|
|
|
246
|
22 |
|
return $service; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $key |
251
|
|
|
* |
252
|
|
|
* @return $this |
253
|
|
|
*/ |
254
|
1 |
|
public function removeService($key) |
255
|
|
|
{ |
256
|
1 |
|
if (isset($this->services[$key])) { |
257
|
1 |
|
unset($this->services[$key]); |
258
|
1 |
|
} |
259
|
|
|
|
260
|
1 |
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return BaseService|null |
265
|
|
|
*/ |
266
|
1 |
|
public function getPreferredService() |
267
|
|
|
{ |
268
|
1 |
|
return $this->getService($this->config['preferred_order'][0]); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Perform Multi-service search |
273
|
|
|
* |
274
|
|
|
* @param $argument |
275
|
|
|
* @param $type |
276
|
|
|
* @param null $servicesArg |
277
|
|
|
* |
278
|
|
|
* @return ArrayCollection |
279
|
|
|
* @throws \Exception |
280
|
|
|
*/ |
281
|
11 |
|
public function doSearch($argument, $type, $servicesArg = null) |
282
|
|
|
{ |
283
|
11 |
|
$services = $this->_prepareSearch($servicesArg); |
284
|
10 |
|
$results = new ArrayCollection(); |
285
|
|
|
|
286
|
10 |
|
foreach ($services as $serviceKey => $service) { |
287
|
10 |
|
$methodName = $this->getMethodName($type); |
288
|
|
|
|
289
|
10 |
|
if (!method_exists($service, $methodName)) { |
290
|
1 |
|
throw new \Exception(sprintf('Method (%s) not found in %s', $methodName, get_class($service))); |
291
|
|
|
} |
292
|
|
|
|
293
|
9 |
|
$results->set($serviceKey, $service->{$methodName}()->getByName($argument)); |
294
|
9 |
|
} |
295
|
|
|
|
296
|
9 |
|
return $results; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Return an arraycollection with (loaded) services |
301
|
|
|
* |
302
|
|
|
* @param mixed $servicesArg |
303
|
|
|
* |
304
|
|
|
* @return ArrayCollection |
305
|
|
|
* @throws \Exception |
306
|
|
|
*/ |
307
|
11 |
|
protected function _prepareSearch($servicesArg = null) |
308
|
|
|
{ |
309
|
11 |
|
$services = new ArrayCollection(); |
310
|
|
|
|
311
|
11 |
|
if (null === $servicesArg) { |
312
|
2 |
|
$services = $this->getServices(); |
313
|
11 |
|
} elseif (is_array($servicesArg)) { |
314
|
6 |
|
foreach ($servicesArg as $service) { |
315
|
6 |
|
if (is_string($service) && $loadedService = $this->getService($service)) { |
316
|
5 |
|
$services->set($service, $loadedService); |
317
|
5 |
|
} else { |
318
|
1 |
|
throw new \Exception(sprintf('Service (%s) cannot be found', $service)); |
319
|
|
|
} |
320
|
5 |
|
} |
321
|
8 |
|
} elseif (is_string($servicesArg) && $loadedService = $this->getService($servicesArg)) { |
322
|
3 |
|
$services->set($servicesArg, $loadedService); |
323
|
|
|
|
324
|
3 |
|
} |
325
|
|
|
|
326
|
10 |
|
return $services; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|