1 | <?php |
||
21 | class MusicInfo |
||
22 | { |
||
23 | use PropertyTrait; |
||
24 | /** |
||
25 | * @var ClientInterface |
||
26 | */ |
||
27 | protected $client; |
||
28 | |||
29 | /** |
||
30 | * @var ArrayCollection |
||
31 | */ |
||
32 | protected $services; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $config; |
||
38 | |||
39 | /** |
||
40 | * Supported Services |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $supportedServices = []; |
||
44 | |||
45 | /** |
||
46 | * MusicInfo constructor. |
||
47 | * |
||
48 | * @param $config |
||
49 | * |
||
50 | * @throws ServiceConfigurationException if musicinfo.service is missing |
||
51 | */ |
||
52 | 39 | public function __construct($config) |
|
53 | { |
||
54 | 39 | $this->config = $config; |
|
55 | |||
56 | 39 | $this->services = new ArrayCollection(); |
|
57 | 39 | $this->setClient( |
|
58 | 39 | new Client($config['defaults']['guzzle']) |
|
59 | 39 | ); |
|
60 | |||
61 | 39 | if (isset($config['services'])) { |
|
62 | 39 | foreach ($config['services'] as $service) { |
|
63 | 39 | if (!isset($config['init_services'])) { |
|
64 | 2 | $config['init_services'] = null; |
|
65 | 2 | } |
|
66 | 39 | $this->loadService($service, $config['init_services']); |
|
67 | 39 | $this->supportedServices[] = $service; |
|
68 | 39 | } |
|
69 | 39 | } else { |
|
70 | 1 | throw new ServiceConfigurationException("musicinfo.services is required"); |
|
71 | } |
||
72 | 39 | } |
|
73 | |||
74 | /** |
||
75 | * @param ClientInterface $client |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | 39 | public function setClient(ClientInterface $client) |
|
80 | { |
||
81 | 39 | $this->client = $client; |
|
82 | |||
83 | 39 | return $this; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return ClientInterface |
||
88 | */ |
||
89 | 39 | public function getClient() |
|
90 | { |
||
91 | 39 | return $this->client; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Load service |
||
96 | * |
||
97 | * @param $service |
||
98 | * @param $init |
||
99 | * |
||
100 | * @return IMusicService |
||
101 | * |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | 39 | public function loadService($service, $init = false) |
|
105 | { |
||
106 | 39 | $fqcn = implode('\\', ['Pbxg33k', 'MusicInfo', 'Service', $service, 'Service']); |
|
107 | 39 | if (class_exists($fqcn)) { |
|
108 | /** @var IMusicService $client */ |
||
109 | 39 | $client = new $fqcn(); |
|
110 | 39 | $client->setConfig($this->mergeConfig($service)); |
|
111 | 39 | $client->setClient($this->getClient()); |
|
112 | 39 | if ($init === true) { |
|
113 | 39 | $client->init(); |
|
114 | 39 | } |
|
115 | 39 | $this->addService($client, $service); |
|
116 | |||
117 | 39 | return $service; |
|
118 | } else { |
||
119 | 1 | throw new \Exception('Service class does not exist: ' . $service . ' (' . $fqcn . ')'); |
|
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Merge shared config with service specific configuration |
||
125 | * |
||
126 | * @param $service |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 39 | public function mergeConfig($service) |
|
131 | { |
||
132 | 39 | $service = strtolower($service); |
|
133 | 39 | if (isset($this->config['service_configuration'][$service])) { |
|
134 | 39 | $config = array_merge( |
|
135 | 39 | $this->config['defaults'], |
|
136 | 39 | $this->config['service_configuration'][$service] |
|
137 | 39 | ); |
|
138 | |||
139 | 39 | return $config; |
|
140 | } else { |
||
141 | 7 | return $this->config['defaults']; |
|
142 | } |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Load all services |
||
147 | * |
||
148 | * @param bool $initialize |
||
149 | * |
||
150 | * @return ArrayCollection |
||
151 | * @throws \Exception |
||
152 | */ |
||
153 | 5 | public function loadServices($initialize = false) |
|
154 | { |
||
155 | 5 | foreach ($this->supportedServices as $service) { |
|
156 | 5 | $this->loadService($service, $initialize); |
|
157 | 5 | } |
|
158 | |||
159 | 5 | return $this->getServices(); |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param IMusicService $service |
||
164 | * @param $key |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | 39 | public function addService(IMusicService $service, $key) |
|
169 | { |
||
170 | 39 | $this->services[strtolower($key)] = $service; |
|
171 | |||
172 | 39 | return $this; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * @return ArrayCollection |
||
177 | */ |
||
178 | 6 | public function getServices() |
|
182 | |||
183 | /** |
||
184 | * @param $key |
||
185 | * |
||
186 | * @return BaseService|null |
||
187 | */ |
||
188 | 24 | public function getService($key) |
|
197 | |||
198 | /** |
||
199 | * @param BaseService $service |
||
200 | * |
||
201 | * @return BaseService |
||
202 | * @throws ServiceConfigurationException |
||
203 | */ |
||
204 | 22 | public function initializeService(BaseService $service) |
|
205 | { |
||
206 | 22 | if (!$service->isInitialized()) { |
|
207 | 1 | $service->init(); |
|
208 | 1 | } |
|
209 | |||
210 | 22 | return $service; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param string $key |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | 1 | public function removeService($key) |
|
226 | |||
227 | /** |
||
228 | * @return BaseService|null |
||
229 | */ |
||
230 | 1 | public function getPreferredService() |
|
234 | |||
235 | /** |
||
236 | * Perform Multi-service search |
||
237 | * |
||
238 | * @param $argument |
||
239 | * @param $type |
||
240 | * @param null $servicesArg |
||
241 | * |
||
242 | * @return ArrayCollection |
||
243 | * @throws \Exception |
||
244 | */ |
||
245 | 11 | public function doSearch($argument, $type, $servicesArg = null) |
|
262 | |||
263 | /** |
||
264 | * Return an arraycollection with (loaded) services |
||
265 | * |
||
266 | * @param mixed $servicesArg |
||
267 | * |
||
268 | * @return ArrayCollection |
||
269 | * @throws \Exception |
||
270 | */ |
||
271 | 11 | protected function _prepareSearch($servicesArg = null) |
|
292 | } |