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

Endpoints::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ElasticsearchModule\Container;
4
5
use Elasticsearch\Endpoints\AbstractEndpoint;
6
use Elasticsearch\Serializers\SerializerInterface;
7
use Elasticsearch\Transport;
8
use ElasticsearchModule\Container\Exception\EndpointNotFoundException;
9
10
/**
11
 * @author Pedro Alves <[email protected]>
12
 */
13
class Endpoints implements EndpointsInterface
14
{
15
    
16
    const ENDPOINTS_NAMESPACE = "\\Elasticsearch\\Endpoints\\";
17
    
18
    /**
19
     * @var Transport
20
     */
21
    private $transport;
22
    
23
    /**
24
     * @var SerializerInterface
25
     */
26
    private $serializer;
27
    
28
    /**
29
     * @var AbstractEndpoint[]
30
     */
31
    private $instances = [];
32
    
33
    /**
34
     * {@inheritDoc}
35
     */
36 6
    public function __construct(Transport $transport, SerializerInterface $serializer)
37
    {
38 6
        $this->transport = $transport;
39 6
        $this->serializer = $serializer;
40 6
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 3
    public function __invoke($name)
46
    {
47 3
        if (!$this->has($name)) {
48 1
            throw new EndpointNotFoundException("The '$name' endpoint does not exist");
49
        }
50 2
        return $this->get($name);
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 2
    public function get($id)
57
    {
58 2
        $endpointClass = $this->getEndpointClass($id);
59 2
        $endpointId = $this->getEndpointId($endpointClass);
60
        
61 2
        if ($this->hasInstance($endpointId)) {
62 1
            return $this->instances[$endpointId];
63
        }
64
        
65 2
        $instance = $this->getNewInstance($id, $endpointClass);
66 2
        $this->instances[$endpointId] = $instance;
67 2
        return $instance;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 3
    public function has($id)
74
    {
75 3
        $endpointClass = $this->getEndpointClass($id);
76 3
        return class_exists($endpointClass);
77
    }
78
79
    /**
80
     * @param string $endpointClass
81
     * @return string
82
     */
83 2
    private function getEndpointId($endpointClass)
84
    {
85 2
        $endpointId = strtolower($endpointClass);
86 2
        return preg_replace('/[^a-z0-9]/', '', $endpointId);
87
    }
88
    
89
    /**
90
     * @param string $id
91
     * @return string
92
     */
93 3
    private function getEndpointClass($id)
94
    {
95 3
        return self::ENDPOINTS_NAMESPACE . $id;
96
    }
97
    
98
    /**
99
     * @param string $endpointId
100
     * @return bool
101
     */
102 2
    private function hasInstance($endpointId)
103
    {
104 2
        return isset($this->instances[$endpointId]);
105
    }
106
    
107
    /**
108
     * @param string $id
109
     * @param string $endpointClass
110
     * @return AbstractEndpoint
111
     */
112 2
    private function getNewInstance($id, $endpointClass)
113
    {
114 2
        if (in_array($id, ['Bulk', 'MSearch', 'MPercolate'])) {
115 2
            return new $endpointClass($this->transport, $this->serializer);
116
        } else {
117 2
            return new $endpointClass($this->transport);
118
        }
119
    }
120
}
121