Passed
Push — feature/spotify ( 233a05...892350 )
by Oguzhan
02:40
created

MusicInfo::removeService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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
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 34
    public function __construct($config)
53
    {
54 34
        $this->config = $config;
55
56 34
        $this->services = new ArrayCollection();
57 34
        $this->setClient(
58 34
            new Client($config['defaults']['guzzle'])
59 34
        );
60
61 34
        if (isset($config['services'])) {
62 34
            foreach ($config['services'] as $service) {
63 34
                if (!isset($config['init_services'])) {
64 2
                    $config['init_services'] = null;
65 1
                }
66 34
                $this->loadService($service, $config['init_services']);
67 34
                $this->supportedServices[] = $service;
68 34
            }
69 34
        } else {
70 1
            throw new ServiceConfigurationException("musicinfo.services is required");
71
        }
72 34
    }
73
74
    /**
75
     * @param ClientInterface $client
76
     *
77
     * @return $this
78
     */
79 34
    public function setClient(ClientInterface $client)
80
    {
81 34
        $this->client = $client;
82
83 34
        return $this;
84
    }
85
86
    /**
87
     * @return ClientInterface
88
     */
89 34
    public function getClient()
90 1
    {
91 34
        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 34
    public function loadService($service, $init = false)
105
    {
106 34
        $fqcn = implode('\\', ['Pbxg33k', 'MusicInfo', 'Service', $service, 'Service']);
107 34
        if (class_exists($fqcn)) {
108
            /** @var IMusicService $client */
109 34
            $client = new $fqcn();
110 34
            $client->setConfig($this->mergeConfig($service));
111 34
            $client->setClient($this->getClient());
112 34
            if ($init === true) {
113 34
                $client->init();
114 34
            }
115 34
            $this->addService($client, $service);
116
117 34
            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 34
    public function mergeConfig($service)
131
    {
132 34
        $service = strtolower($service);
133 34
        if (isset($this->config['service_configuration'][$service])) {
134 34
            $config = array_merge(
135 34
                $this->config['defaults'],
136 34
                $this->config['service_configuration'][$service]
137 34
            );
138
139 34
            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 4
    public function loadServices($initialize = false)
154
    {
155 4
        foreach ($this->supportedServices as $service) {
156 4
            $this->loadService($service, $initialize);
157 4
        }
158
159 4
        return $this->getServices();
160
    }
161
162
    /**
163
     * @param IMusicService $service
164
     * @param               $key
165
     *
166
     * @return $this
167
     */
168 34
    public function addService(IMusicService $service, $key)
169
    {
170 34
        $this->services[strtolower($key)] = $service;
171
172 34
        return $this;
173
    }
174
175
    /**
176
     * @return ArrayCollection
177
     */
178 5
    public function getServices()
179
    {
180 5
        return $this->services;
181
    }
182
183
    /**
184
     * @param $key
185
     *
186
     * @return BaseService|null
187
     */
188 19
    public function getService($key)
189
    {
190 19
        $key = strtolower($key);
191 19
        if (isset($this->services[$key])) {
192 17
            return $this->initializeService($this->services[$key]);
193
        } else {
194 3
            return null;
195
        }
196
    }
197
198
    /**
199
     * @param BaseService $service
200
     *
201
     * @return BaseService
202
     * @throws ServiceConfigurationException
203
     */
204 17
    public function initializeService(BaseService $service)
205
    {
206 17
        if (!$service->isInitialized()) {
207
            $service->init();
208
        }
209
210 17
        return $service;
211
    }
212
213
    /**
214
     * @param string $key
215
     *
216
     * @return $this
217
     */
218 1
    public function removeService($key)
219
    {
220 1
        if (isset($this->services[$key])) {
221 1
            unset($this->services[$key]);
222 1
        }
223
224 1
        return $this;
225
    }
226
227
    /**
228
     * @return BaseService|null
229
     */
230 1
    public function getPreferredService()
231
    {
232 1
        return $this->getService($this->config['preferred_order'][0]);
233
    }
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)
246
    {
247 11
        $services = $this->_prepareSearch($servicesArg);
248 10
        $results = new ArrayCollection();
249
250 10
        foreach ($services as $serviceKey => $service) {
251 10
            $methodName = $this->getMethodName($type);
252
253 10
            if (!method_exists($service, $methodName)) {
254 1
                throw new \Exception(sprintf('Method (%s) not found in %s', $methodName, get_class($service)));
255
            }
256 9
            $results->set($serviceKey, $service->{$methodName}()->getByName($argument));
257 9
        }
258
259 9
        return $results;
260
    }
261
262
    /**
263
     * Return an arraycollection with (loaded) services
264
     *
265
     * @param mixed $servicesArg
266
     *
267
     * @return ArrayCollection
268
     * @throws \Exception
269
     */
270 11
    protected function _prepareSearch($servicesArg = null)
271
    {
272 11
        $services = new ArrayCollection();
273
274 11
        if (null === $servicesArg) {
275 2
            $services = $this->getServices();
276 11
        } elseif (is_array($servicesArg)) {
277 6
            foreach ($servicesArg as $service) {
278 6
                if (is_string($service) && $loadedService = $this->getService($service)) {
279 5
                    $services->set($service, $loadedService);
280 5
                } else {
281 1
                    throw new \Exception(sprintf('Service (%s) cannot be found', $service));
282
                }
283 5
            }
284 8
        } elseif (is_string($servicesArg) && $loadedService = $this->getService($servicesArg)) {
285 3
            $services->set($servicesArg, $loadedService);
286
287 3
        }
288
289 10
        return $services;
290
    }
291
}