getConnectionsFromConfiguration()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

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