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

TransportFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 21.88 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 7
loc 32
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 7 17 5
A getServiceType() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ElasticsearchModule\Service;
4
5
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
6
use Elasticsearch\Transport;
7
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
8
use Zend\ServiceManager\ServiceLocatorInterface;
9
10
/**
11
 * @author Pedro Alves <[email protected]>
12
 */
13
class TransportFactory extends AbstractFactory
14
{
15
 
16
    /**
17
     * {@inheritDoc}
18
     */
19 10
    protected function create(ServiceLocatorInterface $serviceLocator, $config)
20
    {
21 10
        $retries = isset($config['retries']) ? $config['retries'] : 2;
22 10
        $sniffOnStart = isset($config['sniff_on_start']) ? $config['sniff_on_start'] : false;
23 10
        $connectionPool = $serviceLocator->get($config['connection_pool']);
24
        
25 10 View Code Duplication
        if (!$connectionPool instanceof AbstractConnectionPool) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26 1
            throw new ServiceNotCreatedException(sprintf(
27 1
                "The connection pool must be an instance of %s, %s given",
28 1
                AbstractConnectionPool::class,
29 1
                is_object($connectionPool) ? get_class($connectionPool) : gettype($connectionPool)
30 1
            ));
31
        }
32 9
        $loggers = $serviceLocator->get($config['loggers']);
33
        
34 9
        return new Transport($retries, $sniffOnStart, $connectionPool, $loggers['logger']);
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 10
    public function getServiceType()
41
    {
42 10
        return 'transport';
43
    }
44
}
45