Completed
Push — master ( 348af9...23656b )
by Walter
05:14 queued 02:58
created

DispatcherFactory::createService()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of phpab/phpab-module. (https://github.com/phpab/phpab-module)
4
 *
5
 * @link https://github.com/phpab/phpab-module for the canonical source repository
6
 * @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/)
7
 * @license https://raw.githubusercontent.com/phpab/phpab-module/master/LICENSE MIT
8
 */
9
10
namespace PhpAbModule\Service;
11
12
use PhpAb\Engine\Engine;
13
use PhpAb\Event\Dispatcher;
14
use PhpAb\Event\SubscriberInterface;
15
use PhpAb\Storage\Cookie;
16
use PhpAb\Storage\Runtime;
17
use PhpAb\Storage\Session;
18
use RuntimeException;
19
use Zend\ServiceManager\FactoryInterface;
20
use Zend\ServiceManager\ServiceLocatorInterface;
21
22
class DispatcherFactory implements FactoryInterface
23
{
24
    public function createService(ServiceLocatorInterface $serviceLocator)
25
    {
26
        $dispatcher = new Dispatcher();
27
28
        $config = $serviceLocator->get('Config');
29
        $collectorName = $config['phpab']['analytics']['collector'];
30
31
        if ($collectorName && $serviceLocator->has($collectorName)) {
32
            $dataCollector = $serviceLocator->get($collectorName);
33
34
            if (!$dataCollector instanceof SubscriberInterface) {
35
                throw new RuntimeException(sprintf(
36
                    'The data collector is not an instance of %s',
37
                    SubscriberInterface::class
38
                ));
39
            }
40
41
            $dispatcher->addSubscriber($dataCollector);
42
        }
43
44
        return $dispatcher;
45
    }
46
}
47