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

ConnectionPoolFactory::extractURIParts()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 3.0416
1
<?php
2
3
namespace ElasticsearchModule\Service;
4
5
use ArrayObject;
6
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
7
use Elasticsearch\Connections\ConnectionFactoryInterface;
8
use Elasticsearch\Connections\ConnectionInterface;
9
use Zend\ServiceManager\Exception\InvalidArgumentException;
10
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use Zend\Stdlib\ArrayObject as ZendArrayObject;
13
14
/**
15
 * @author Pedro Alves <[email protected]>
16
 */
17
class ConnectionPoolFactory extends AbstractFactory
18
{
19
    
20
    /**
21
     * {@inheritDoc}
22
     */
23 20
    protected function create(ServiceLocatorInterface $serviceLocator, $config)
24
    {
25 20
        $className = $config['class'];
26
        
27 20
        if (!in_array(AbstractConnectionPool::class, class_parents($className))) {
28 1
            throw new ServiceNotCreatedException(sprintf(
29 1
                "'%s' does not implement %s",
30 1
                $className,
31
                AbstractConnectionPool::class
32 1
            ));
33
        }
34 19
        $selector = $this->getServiceOrClassObject($serviceLocator, $config['selector']);
35 19
        $connectionFactory = $this->getServiceOrClassObject($serviceLocator, $config['connection_factory']);
36
        
37 19 View Code Duplication
        if (!$connectionFactory instanceof ConnectionFactoryInterface) {
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...
38 1
            throw new ServiceNotCreatedException(sprintf(
39 1
                "The connection factory must be an instace of %s, %s, given",
40 1
                ConnectionFactoryInterface::class,
41 1
                is_object($connectionFactory) ? get_class($connectionFactory) : gettype($connectionFactory)
42 1
            ));
43
        }
44 18
        $parameters = isset($config['parameters']) ? $config['parameters'] : [];
45 18
        $connections = $this->getConnectionsFromConfiguration($config, $connectionFactory);
46 11
        return new $className(
47 11
            $connections,
48 11
            $selector,
49 11
            $connectionFactory,
50
            $parameters
51 11
        );
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 20
    public function getServiceType()
58 1
    {
59 20
        return 'connection_pool';
60
    }
61
    
62
    /**
63
     * @param array|ArrayObject|ZendArrayObject $config
64
     * @param ConnectionFactoryInterface $connectionFactory
65
     * @return ConnectionInterface[]
66
     * @throws ServiceNotCreatedException
67
     */
68 18
    private function getConnectionsFromConfiguration($config, ConnectionFactoryInterface $connectionFactory)
69
    {
70 18
        $connections = [];
71
        
72 18
        foreach ($config['hosts'] as $host) {
73 8
            if (!is_string($host) && !is_array($host)) {
74 6
                throw new ServiceNotCreatedException("Could not parse host: " . print_r($host, true));
75
            }
76
            
77 2
            if (is_string($host)) {
78 1
                $host = $this->extractURIParts($host);
79 1
            }
80 2
            $host = $this->normalizeExtendedHost($host);
81 1
            $connections[] = $connectionFactory->create($host);
82 11
        }
83 11
        return $connections;
84
    }
85
86
    /**
87
     * @param $host
88
     * @return array
89
     */
90 2
    private function normalizeExtendedHost($host) {
91 2
        if (isset($host['host']) === false) {
92 1
            throw new ServiceNotCreatedException(
93 1
                "Required 'host' was not defined in extended format: " . print_r($host, true)
94 1
            );
95
        }
96 1
        return array_merge(['scheme' => 'http', 'port' => 9200], $host);
97
    }
98
99
    /**
100
     * @param string $host
101
     *
102
     * @throws InvalidArgumentException
103
     * @return array
104
     */
105 1
    private function extractURIParts($host)
106
    {
107 1
        $host = filter_var($host, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) ? $host : 'http://' . $host;
108 1
        $parts = parse_url($host);
109
110 1
        if ($parts === false) {
111
            throw new InvalidArgumentException("Could not parse URI");
112
        }
113 1
        return array_merge(['port' => 9200], $parts);
114
    }
115
116
}
117