Passed
Branch master (feff43)
by Pedro
04:38 queued 22s
created

EndpointFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 48
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 3
A getServiceType() 0 4 1
A getInvalidTypeException() 0 9 2
1
<?php
2
3
namespace ElasticsearchModule\Service;
4
5
use Elasticsearch\Serializers\SerializerInterface;
6
use ElasticsearchModule\Container\EndpointsInterface;
7
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
8
use Zend\ServiceManager\ServiceLocatorInterface;
9
10
/**
11
 * @author Pedro Alves <[email protected]>
12
 */
13
class EndpointFactory extends AbstractFactory
14
{
15
    
16
    /**
17
     * {@inheritDoc}
18
     */
19 6
    protected function create(ServiceLocatorInterface $serviceLocator, $config)
20
    {
21 6
        $serializer = $this->getServiceOrClassObject($serviceLocator, $config['serializer']);
22
        
23 6
        if (!$serializer instanceof SerializerInterface) {
24 1
            throw $this->getInvalidTypeException('serializer', SerializerInterface::class, $serializer);
25
        }
26 5
        $transport = $serviceLocator->get($config['transport']);
27 5
        $container = $config['container'];
28 5
        $container = new $container($transport, $serializer);
29
        
30 5
        if (!$container instanceof EndpointsInterface) {
31 1
            throw $this->getInvalidTypeException('container', EndpointsInterface::class, $container);
32
        }
33
        
34 4
        return $container;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 6
    public function getServiceType()
41
    {
42 6
        return 'endpoint';
43
    }
44
    
45
    /**
46
     * @param string $name
47
     * @param string $className
48
     * @param mixed $var
49
     * @return ServiceNotCreatedException
50
     */
51 2
    private function getInvalidTypeException($name, $className, $var)
52
    {
53 2
        return new ServiceNotCreatedException(sprintf(
54 2
            "The %s must be instance of %s, %s given",
55 2
            $name,
56 2
            $className,
57 2
            is_object($var) ? get_class($var) : gettype($var)
58 2
        ));
59
    }
60
}