Completed
Pull Request — master (#14)
by Pedro
05:20
created

AbstractFactory::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\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
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @param string $name
25
     */
26 43
    public function __construct($name)
27 2
    {
28 43
        $this->name = $name;
29 43
    }
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 42
    public function createService(ServiceLocatorInterface $serviceLocator)
48
    {
49 42
        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 43
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
58
    {
59 43
        $config = $container->get('Config');
60 43
        $serviceType = $this->getServiceType();
61
        
62 43
        if (!isset($config['elasticsearch'][$serviceType][$this->name])) {
63 3
            throw new ServiceNotCreatedException("$requestedName could not be found");
64
        }
65
        
66 40
        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 34
    protected function getServiceOrClassObject(ContainerInterface $container, $serviceOrClass)
75
    {
76 34
        if ($container->has($serviceOrClass)) {
77 22
            return $container->get($serviceOrClass);
78 31
        } else if (class_exists($serviceOrClass)) {
79 28
            return new $serviceOrClass();
80
        }
81 3
        return null;
82
    }
83
    
84
    /**
85
     * @return string
86
     */
87 23
    protected function getName()
88
    {
89 23
        return $this->name;
90
    }
91
}
92