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 Pbxg33k\Traits\PropertyTrait; |
11
|
|
|
|
12
|
|
|
class MusicInfo |
13
|
|
|
{ |
14
|
|
|
use PropertyTrait; |
15
|
|
|
/** |
16
|
|
|
* @var ClientInterface |
17
|
|
|
*/ |
18
|
|
|
protected $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ArrayCollection |
22
|
|
|
*/ |
23
|
|
|
protected $services; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $config; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Supported Services |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $supportedServices = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* MusicInfo constructor. |
38
|
|
|
* @param $config |
39
|
|
|
* @throws ServiceConfigurationException if musicinfo.service is missing |
40
|
|
|
*/ |
41
|
19 |
|
public function __construct($config) |
42
|
|
|
{ |
43
|
19 |
|
$this->config = $config; |
44
|
|
|
|
45
|
19 |
|
$this->services = new ArrayCollection(); |
46
|
19 |
|
$this->setClient( |
47
|
19 |
|
new Client($config['defaults']['guzzle']) |
48
|
19 |
|
); |
49
|
|
|
|
50
|
19 |
|
if (isset($config['services'])) { |
51
|
19 |
|
foreach ($config['services'] as $service) { |
52
|
19 |
|
if (!isset($config['init_services'])) { |
53
|
1 |
|
$config['init_services'] = null; |
54
|
1 |
|
} |
55
|
19 |
|
$this->loadService($service, $config['init_services']); |
56
|
19 |
|
$this->supportedServices[] = $service; |
57
|
19 |
|
} |
58
|
19 |
|
} else { |
59
|
1 |
|
throw new ServiceConfigurationException("musicinfo.services is required"); |
60
|
|
|
} |
61
|
19 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ClientInterface $client |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
19 |
|
public function setClient(ClientInterface $client) |
69
|
|
|
{ |
70
|
19 |
|
$this->client = $client; |
71
|
|
|
|
72
|
19 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return ClientInterface |
77
|
|
|
*/ |
78
|
19 |
|
public function getClient() |
79
|
|
|
{ |
80
|
19 |
|
return $this->client; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Load service |
85
|
|
|
* |
86
|
|
|
* @param $service |
87
|
|
|
* @param $init |
88
|
|
|
* |
89
|
|
|
* @return IMusicService |
90
|
|
|
* |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
19 |
|
public function loadService($service, $init = false) |
94
|
|
|
{ |
95
|
19 |
|
$fqcn = implode('\\', ['Pbxg33k', 'MusicInfo', 'Service', $service, 'Service']); |
96
|
19 |
|
if (class_exists($fqcn)) { |
97
|
|
|
/** @var IMusicService $client */ |
98
|
19 |
|
$client = new $fqcn(); |
99
|
19 |
|
$client->setConfig($this->mergeConfig($service)); |
100
|
19 |
|
$client->setClient($this->getClient()); |
101
|
19 |
|
if ($init == true) { |
|
|
|
|
102
|
3 |
|
$client->init(); |
103
|
3 |
|
} |
104
|
19 |
|
$this->addService($client, $service); |
105
|
19 |
|
return $service; |
106
|
|
|
} else { |
107
|
1 |
|
throw new \Exception('Service class does not exist: ' . $service . ' (' . $fqcn . ')'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Merge shared config with service specific configuration |
113
|
|
|
* |
114
|
|
|
* @param $service |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
19 |
|
public function mergeConfig($service) |
118
|
|
|
{ |
119
|
19 |
|
$service = strtolower($service); |
120
|
19 |
|
if (isset($this->config['service_configuration'][$service])) { |
121
|
19 |
|
$config = array_merge( |
122
|
19 |
|
$this->config['defaults'], |
123
|
19 |
|
$this->config['service_configuration'][$service] |
124
|
19 |
|
); |
125
|
19 |
|
return $config; |
126
|
|
|
} else { |
127
|
1 |
|
return $this->config['defaults']; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Load all services |
133
|
|
|
* |
134
|
|
|
* @param bool $initialize |
135
|
|
|
* |
136
|
|
|
* @return ArrayCollection |
137
|
|
|
* @throws \Exception |
138
|
|
|
*/ |
139
|
3 |
|
public function loadServices($initialize = false) |
140
|
|
|
{ |
141
|
3 |
|
foreach ($this->supportedServices as $service) { |
142
|
3 |
|
$this->loadService($service, $initialize); |
143
|
3 |
|
} |
144
|
|
|
|
145
|
3 |
|
return $this->getServices(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param IMusicService $service |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
19 |
|
public function addService(IMusicService $service, $key) |
154
|
|
|
{ |
155
|
19 |
|
$this->services[strtolower($key)] = $service; |
156
|
19 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return ArrayCollection |
161
|
|
|
*/ |
162
|
3 |
|
public function getServices() |
163
|
|
|
{ |
164
|
3 |
|
return $this->services; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param $key |
169
|
|
|
* @return BaseService|null |
170
|
|
|
*/ |
171
|
11 |
|
public function getService($key) |
172
|
|
|
{ |
173
|
11 |
|
$key = strtolower($key); |
174
|
11 |
|
if (isset($this->services[$key])) { |
175
|
10 |
|
return $this->initializeService($this->services[$key]); |
176
|
|
|
} else { |
177
|
2 |
|
return null; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param BaseService $service |
183
|
|
|
* |
184
|
|
|
* @return BaseService |
185
|
|
|
* @throws ServiceConfigurationException |
186
|
|
|
*/ |
187
|
10 |
|
public function initializeService(BaseService $service) |
188
|
|
|
{ |
189
|
10 |
|
if (!$service->isInitialized()) { |
190
|
10 |
|
$service->init(); |
191
|
10 |
|
} |
192
|
|
|
|
193
|
10 |
|
return $service; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $key |
198
|
|
|
* |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
1 |
|
public function removeService($key) |
202
|
|
|
{ |
203
|
1 |
|
if (isset($this->services[$key])) { |
204
|
1 |
|
unset($this->services[$key]); |
205
|
1 |
|
} |
206
|
|
|
|
207
|
1 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return BaseService|null |
212
|
|
|
*/ |
213
|
1 |
|
public function getPreferredService() |
214
|
|
|
{ |
215
|
1 |
|
return $this->getService($this->config['preferred_order'][0]); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Perform Multi-service search |
220
|
|
|
* |
221
|
|
|
* @param $argument |
222
|
|
|
* @param $type |
223
|
|
|
* @param null $servicesArg |
224
|
|
|
* |
225
|
|
|
* @return ArrayCollection |
226
|
|
|
* @throws \Exception |
227
|
|
|
*/ |
228
|
5 |
|
public function doSearch($argument, $type, $servicesArg = null) |
229
|
|
|
{ |
230
|
5 |
|
$services = $this->_prepareSearch($servicesArg); |
231
|
5 |
|
$results = new ArrayCollection(); |
232
|
|
|
|
233
|
5 |
|
foreach ($services as $serviceKey => $service) { |
234
|
5 |
|
$methodName = $this->getMethodName($type); |
235
|
|
|
|
236
|
5 |
|
if (!method_exists($service, $methodName)) { |
237
|
|
|
throw new \Exception(sprintf('Method (%s) not found in %s', $methodName, get_class($service))); |
238
|
|
|
} |
239
|
5 |
|
$results->set($serviceKey, $service->{$methodName}()->getByName($argument)); |
240
|
5 |
|
} |
241
|
|
|
|
242
|
5 |
|
return $results; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Return an arraycollection with (loaded) services |
247
|
|
|
* |
248
|
|
|
* @param mixed $servicesArg |
249
|
|
|
* |
250
|
|
|
* @return ArrayCollection |
251
|
|
|
* @throws \Exception |
252
|
|
|
*/ |
253
|
5 |
|
protected function _prepareSearch($servicesArg = null) |
254
|
|
|
{ |
255
|
5 |
|
$services = new ArrayCollection(); |
256
|
|
|
|
257
|
5 |
|
if (null === $servicesArg) { |
258
|
1 |
|
$services = $this->getServices(); |
259
|
5 |
|
} elseif (is_array($servicesArg)) { |
260
|
2 |
|
foreach ($servicesArg as $service) { |
261
|
2 |
|
if (is_string($service) && $loadedService = $this->getService($service)) { |
262
|
2 |
|
$services->set($service, $loadedService); |
263
|
2 |
|
} else { |
264
|
|
|
throw new \Exception(sprintf('Service (%s) cannot be found', $service)); |
265
|
|
|
} |
266
|
2 |
|
} |
267
|
4 |
|
} elseif (is_string($servicesArg) && $loadedService = $this->getService($servicesArg)) { |
268
|
2 |
|
$services->set($servicesArg, $loadedService); |
269
|
|
|
|
270
|
2 |
|
} |
271
|
|
|
|
272
|
5 |
|
return $services; |
273
|
|
|
} |
274
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.