ConnectionFactory::getServiceType()   A
last analyzed

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 Elasticsearch\Connections\ConnectionFactoryInterface;
7
use Interop\Container\ContainerInterface;
8
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
9
use Zend\Stdlib\ArrayObject as ZendArrayObject;
10
11
/**
12
 * @author Pedro Alves <[email protected]>
13
 */
14
class ConnectionFactory extends AbstractFactory
15
{
16
    
17
    /**
18
     * {@inheritDoc}
19
     */
20 22
    protected function create(ContainerInterface $container, $config)
21
    {
22 22
        $factoryName = $config['factory'];
23 22
        $handler = $container->get($config['handler']);
24 22
        $loggers = $container->get($config['loggers']);
25 22
        $serializer = $this->getServiceOrClassObject($container, $config['serializer']);
26 22
        $params = $this->getConnectionParametersFromConfiguration($config);
27 22
        $connectionFactory = new $factoryName($handler, $params, $serializer, $loggers['logger'], $loggers['tracer']);
28
        
29 22
        if (!$connectionFactory instanceof ConnectionFactoryInterface) {
30 1
            throw new ServiceNotCreatedException(sprintf(
31 1
                "The '%s' class does not implements %s",
32 1
                $factoryName,
33
                ConnectionFactoryInterface::class
34 1
            ));
35
        }
36 21
        return $connectionFactory;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42 22
    public function getServiceType()
43
    {
44 22
        return 'connection_factory';
45
    }
46
    
47
    /**
48
     * @param array|ArrayObject|ZendArrayObject $config
49
     * @return array
50
     */
51 22
    private function getConnectionParametersFromConfiguration($config)
52
    {
53 22
        return (isset($config['params']) && is_array($config['params'])) ? $config['params'] : [];
54
    }
55
56
}
57