1 | <?php |
||
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 | * |
||
39 | * @param $config |
||
40 | * |
||
41 | * @throws ServiceConfigurationException if musicinfo.service is missing |
||
42 | */ |
||
43 | 19 | public function __construct($config) |
|
44 | { |
||
45 | 19 | $this->config = $config; |
|
46 | |||
47 | 19 | $this->services = new ArrayCollection(); |
|
48 | 19 | $this->setClient( |
|
49 | 19 | new Client($config['defaults']['guzzle']) |
|
50 | 19 | ); |
|
51 | |||
52 | 19 | if (isset($config['services'])) { |
|
53 | 19 | foreach ($config['services'] as $service) { |
|
54 | 19 | if (!isset($config['init_services'])) { |
|
55 | 1 | $config['init_services'] = null; |
|
56 | 1 | } |
|
57 | 19 | $this->loadService($service, $config['init_services']); |
|
58 | 19 | $this->supportedServices[] = $service; |
|
59 | 19 | } |
|
60 | 19 | } else { |
|
61 | 1 | throw new ServiceConfigurationException("musicinfo.services is required"); |
|
62 | } |
||
63 | 19 | } |
|
64 | |||
65 | /** |
||
66 | * @param ClientInterface $client |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | 19 | public function setClient(ClientInterface $client) |
|
71 | { |
||
72 | 19 | $this->client = $client; |
|
73 | |||
74 | 19 | return $this; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return ClientInterface |
||
79 | */ |
||
80 | 19 | public function getClient() |
|
81 | { |
||
82 | 19 | return $this->client; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Load service |
||
87 | * |
||
88 | * @param $service |
||
89 | * @param $init |
||
90 | * |
||
91 | * @return IMusicService |
||
92 | * |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | 19 | public function loadService($service, $init = false) |
|
96 | { |
||
97 | 19 | $fqcn = implode('\\', ['Pbxg33k', 'MusicInfo', 'Service', $service, 'Service']); |
|
98 | 19 | if (class_exists($fqcn)) { |
|
99 | /** @var IMusicService $client */ |
||
100 | 19 | $client = new $fqcn(); |
|
101 | 19 | $client->setConfig($this->mergeConfig($service)); |
|
102 | 19 | $client->setClient($this->getClient()); |
|
103 | 19 | if ($init === true) { |
|
104 | 3 | $client->init(); |
|
105 | 3 | } |
|
106 | 19 | $this->addService($client, $service); |
|
107 | |||
108 | 19 | return $service; |
|
109 | } else { |
||
110 | 1 | throw new \Exception('Service class does not exist: ' . $service . ' (' . $fqcn . ')'); |
|
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Merge shared config with service specific configuration |
||
116 | * |
||
117 | * @param $service |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 19 | public function mergeConfig($service) |
|
122 | { |
||
123 | 19 | $service = strtolower($service); |
|
124 | 19 | if (isset($this->config['service_configuration'][$service])) { |
|
125 | 19 | $config = array_merge( |
|
126 | 19 | $this->config['defaults'], |
|
127 | 19 | $this->config['service_configuration'][$service] |
|
128 | 19 | ); |
|
129 | |||
130 | 19 | return $config; |
|
131 | } else { |
||
132 | 1 | return $this->config['defaults']; |
|
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Load all services |
||
138 | * |
||
139 | * @param bool $initialize |
||
140 | * |
||
141 | * @return ArrayCollection |
||
142 | * @throws \Exception |
||
143 | */ |
||
144 | 3 | public function loadServices($initialize = false) |
|
145 | { |
||
146 | 3 | foreach ($this->supportedServices as $service) { |
|
147 | 3 | $this->loadService($service, $initialize); |
|
148 | 3 | } |
|
149 | |||
150 | 3 | return $this->getServices(); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param IMusicService $service |
||
155 | * @param $key |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | 19 | public function addService(IMusicService $service, $key) |
|
160 | { |
||
161 | 19 | $this->services[strtolower($key)] = $service; |
|
162 | |||
163 | 19 | return $this; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return ArrayCollection |
||
168 | */ |
||
169 | 3 | public function getServices() |
|
173 | |||
174 | /** |
||
175 | * @param $key |
||
176 | * |
||
177 | * @return BaseService|null |
||
178 | */ |
||
179 | 11 | public function getService($key) |
|
180 | { |
||
188 | |||
189 | /** |
||
190 | * @param BaseService $service |
||
191 | * |
||
192 | * @return BaseService |
||
193 | * @throws ServiceConfigurationException |
||
194 | */ |
||
195 | 10 | public function initializeService(BaseService $service) |
|
203 | |||
204 | /** |
||
205 | * @param string $key |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | 1 | public function removeService($key) |
|
217 | |||
218 | /** |
||
219 | * @return BaseService|null |
||
220 | */ |
||
221 | 1 | public function getPreferredService() |
|
225 | |||
226 | /** |
||
227 | * Perform Multi-service search |
||
228 | * |
||
229 | * @param $argument |
||
230 | * @param $type |
||
231 | * @param null $servicesArg |
||
232 | * |
||
233 | * @return ArrayCollection |
||
234 | * @throws \Exception |
||
235 | */ |
||
236 | 5 | public function doSearch($argument, $type, $servicesArg = null) |
|
252 | |||
253 | /** |
||
254 | * Return an arraycollection with (loaded) services |
||
255 | * |
||
256 | * @param mixed $servicesArg |
||
257 | * |
||
258 | * @return ArrayCollection |
||
259 | * @throws \Exception |
||
260 | */ |
||
261 | 5 | protected function _prepareSearch($servicesArg = null) |
|
282 | } |