Passed
Push — master ( 77b471...6dcba4 )
by Pedro
49s
created

AbstractFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
crap 2
1
<?php
2
3
namespace ElasticsearchModule\Service;
4
5
use ArrayObject;
6
use Interop\Container\ContainerInterface;
7
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
8
use Zend\ServiceManager\Factory\FactoryInterface;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
use Zend\Stdlib\ArrayObject as ZendArrayObject;
11
12
/**
13
 * @author Pedro Alves <[email protected]>
14
 */
15
abstract class AbstractFactory implements FactoryInterface
16
{
17
    
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @param string $name
25
     */
26 44
    public function __construct($name)
27 2
    {
28 44
        $this->name = $name;
29 44
    }
30
    
31
    /**
32
     * @return string
33
     */
34
    abstract public function getServiceType();
35
    
36
    /**
37
     * @param ContainerInterface $container
38
     * @param array|ArrayObject|ZendArrayObject $config
39
     */
40
    abstract protected function create(ContainerInterface $container, $config);
41
    
42
    /**
43
     * @param ServiceLocatorInterface $serviceLocator
44
     * @return mixed
45
     * @throws ServiceNotCreatedException
46
     */
47 43
    public function createService(ServiceLocatorInterface $serviceLocator)
48
    {
49 43
        return $this($serviceLocator, sprintf('elasticsearch.%s.%s', $this->getServiceType(), $this->name));
50
    }
51
    
52
    /**
53
     * @param ContainerInterface $container
54
     * @return mixed
55
     * @throws ServiceNotCreatedException
56
     */
57 44
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
58
    {
59 44
        $config = $container->get('Config');
60 44
        $serviceType = $this->getServiceType();
61
        
62 44
        if (!isset($config['elasticsearch'][$serviceType][$this->name])) {
63 3
            throw new ServiceNotCreatedException("$requestedName could not be found");
64
        }
65
        
66 41
        return $this->create($container, $config['elasticsearch'][$serviceType][$this->name]);
67
    }
68
    
69
    /**
70
     * @param ContainerInterface $container
71
     * @param string $serviceOrClass
72
     * @return mixed
73
     */
74 35
    protected function getServiceOrClassObject(ContainerInterface $container, $serviceOrClass)
75
    {
76 35
        if ($container->has($serviceOrClass)) {
77 23
            return $container->get($serviceOrClass);
78 32
        } else if (class_exists($serviceOrClass)) {
79 29
            return new $serviceOrClass();
80
        }
81 3
        return null;
82
    }
83
    
84
    /**
85
     * @return string
86
     */
87 24
    protected function getName()
88
    {
89 24
        return $this->name;
90
    }
91
}
92