ClientFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 3
1
<?php
2
3
namespace ElasticsearchModule\Service;
4
5
use Elasticsearch\Client;
6
use Elasticsearch\Transport;
7
use Interop\Container\ContainerInterface;
8
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
9
10
/**
11
 * @author Pedro Alves <[email protected]>
12
 */
13
class ClientFactory extends AbstractFactory
14
{
15
    use InvalidTypeExceptionTrait;
16
    
17
    /**
18
     * {@inheritDoc}
19
     */
20 4
    protected function create(ContainerInterface $container, $config)
21
    {
22 4
        $transport = $container->get($config['transport']);
23
        
24 4
        if (!$transport instanceof Transport) {
25 1
            throw $this->getException('transport', Transport::class, ServiceNotCreatedException::class, $transport);
26
        }
27 3
        $endpoint = $container->get($config['endpoint']);
28
        
29 3
        if (!is_callable($endpoint)) {
30 1
            throw $this->getException('endpoint', 'callable', ServiceNotCreatedException::class, $endpoint);
31
        }
32 2
        return new Client($transport, $endpoint);
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 4
    public function getServiceType()
39
    {
40 4
        return 'client';
41
    }
42
}
43