Completed
Push — master ( 4a9289...6c8c8c )
by Peter
08:35
created

Manganel   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 15
lcom 3
cbo 6
dl 0
loc 161
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A create() 0 14 2
A fly() 0 12 3
A init() 0 4 1
A drop() 0 12 4
A getClient() 0 16 2
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link http://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel;
14
15
use Elasticsearch\Client;
16
use Elasticsearch\ClientBuilder;
17
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
18
use Maslosoft\EmbeDi\EmbeDi;
19
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionDecorator;
20
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionsDecorator;
21
use Maslosoft\Manganel\Decorators\QueryBuilder\ScrollDecorator;
22
use Maslosoft\Manganel\Decorators\QueryBuilder\SearchDecorator;
23
use Maslosoft\Manganel\Meta\ManganelMeta;
24
25
/**
26
 * Manganel
27
 *
28
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
29
 */
30
class Manganel
31
{
32
33
	const DefaultIndexId = 'manganel';
34
35
	public $decorators = [
36
		SearchCriteria::class => [
37
			ConditionDecorator::class,
38
			ConditionsDecorator::class,
39
			ScrollDecorator::class,
40
			SearchDecorator::class,
41
		]
42
	];
43
	public $hosts = [
44
		'localhost:9200'
45
	];
46
	public $auth = null;
47
	public $username = '';
48
	public $password = '';
49
	public $params = [];
50
51
	/**
52
	 * TODO Enforce lowercase
53
	 */
54
	public $index = 'my_index';
55
	public $indexId = self::DefaultIndexId;
56
57
	/**
58
	 *
59
	 * @var Client
60
	 */
61
	private $client = null;
62
63
	/**
64
	 * Dependency injection container
65
	 * @var EmbeDi
66
	 */
67
	private $di = null;
68
69
	/**
70
	 * Instances of manganel
71
	 * @var Manganel[]
72
	 */
73
	private static $mnl = [];
74
75
	/**
76
	 * Hash map of class name to id. This is to reduce overhead of Mangan::fromModel()
77
	 * @var string[]
78
	 */
79
	private static $classToId = [];
80
81
	/**
82
	 * Class constructor
83
	 * @codeCoverageIgnore This is implicitly tested
84
	 * @param string $indexId
85
	 */
86
	public function __construct($indexId = self::DefaultIndexId)
87
	{
88
		if (empty($indexId))
89
		{
90
			$indexId = self::DefaultIndexId;
91
		}
92
		$this->indexId = $indexId;
93
		$this->di = new EmbeDi($this->indexId);
94
		$this->di->configure($this);
95
96
		if (empty(self::$mnl[$indexId]))
97
		{
98
			self::$mnl[$indexId] = $this;
99
		}
100
	}
101
102
	/**
103
	 * @codeCoverageIgnore This is implicitly tested
104
	 * @param AnnotatedInterface $model
105
	 * @return static
106
	 */
107
	public static function create(AnnotatedInterface $model)
108
	{
109
		$key = get_class($model);
110
		if (isset(self::$classToId[$key]))
111
		{
112
			$indexId = self::$classToId[$key];
113
		}
114
		else
115
		{
116
			$indexId = ManganelMeta::create($model)->type()->indexId;
117
			self::$classToId[$key] = $indexId;
118
		}
119
		return static::fly($indexId);
120
	}
121
122
	/**
123
	 * Get flyweight instance of Manganel component.
124
	 * Only one instance will be created for each `$indexId`.
125
	 *
126
	 * @codeCoverageIgnore This is implicitly tested
127
	 * @new
128
	 * @param string $indexId
129
	 * @return Manganel
130
	 */
131
	public static function fly($indexId = self::DefaultIndexId)
132
	{
133
		if (empty($indexId))
134
		{
135
			$indexId = self::DefaultIndexId;
136
		}
137
		if (empty(self::$mnl[$indexId]))
138
		{
139
			self::$mnl[$indexId] = new static($indexId);
140
		}
141
		return self::$mnl[$indexId];
142
	}
143
144
	/**
145
	 * @codeCoverageIgnore This is implicitly tested
146
	 */
147
	public function init()
148
	{
149
		$this->di->store($this);
150
	}
151
152
	/**
153
	 * Drop current index
154
	 * @return bool
155
	 */
156 1
	public function drop()
157
	{
158
		$params = [
159 1
			'index' => strtolower($this->index)
160 1
		];
161 1
		$result = $this->getClient()->indices()->delete($params);
162 1
		if (is_array($result) && array_key_exists('acknowledged', $result) && $result['acknowledged'])
163 1
		{
164 1
			return true;
165
		}
166
		return false;
167
	}
168
169
	/**
170
	 * @codeCoverageIgnore This is implicitly tested
171
	 * @return Client
172
	 */
173
	public function getClient()
174
	{
175
		if (null === $this->client)
176
		{
177
			$this->params['hosts'] = $this->hosts;
178
			$this->params['connectionParams']['auth'] = [
179
				$this->username,
180
				$this->password,
181
				$this->auth
182
			];
183
			$cb = ClientBuilder::create();
184
			$cb->setHosts($this->hosts);
185
			$this->client = $cb->build();
186
		}
187
		return $this->client;
188
	}
189
190
}
191