Completed
Push — master ( 616fda...ede5da )
by Peter
06:14
created

IndexManager::index()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.5923

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 25
ccs 8
cts 12
cp 0.6667
rs 8.5806
cc 4
eloc 13
nc 4
nop 0
crap 4.5923
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Manganel;
10
11
use Elasticsearch\Client;
12
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
13
use Maslosoft\Mangan\Helpers\CollectionNamer;
14
use Maslosoft\Mangan\Mangan;
15
use Maslosoft\Manganel\Exceptions\ManganelException;
16
use Maslosoft\Manganel\Meta\ManganelMeta;
17
use UnexpectedValueException;
18
19
/**
20
 * IndexMangager
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class IndexManager
25
{
26
27
	/**
28
	 * Manganel instance
29
	 * @var Manganel
30
	 */
31
	private $manganel = null;
32
33
	/**
34
	 * Model meta data
35
	 * @var ManganelMeta
36
	 */
37
	private $meta = null;
38
39
	/**
40
	 * Annotated model
41
	 * @var AnnotatedInterface
42
	 */
43
	private $model;
44
45
	/**
46
	 * Whether model is indexable
47
	 * @var bool
48
	 */
49
	private $isIndexable = false;
50
51 17
	public function __construct($model)
52
	{
53 17
		$this->model = $model;
54 17
		$this->meta = ManganelMeta::create($this->model);
55 17
		if (!empty($this->meta->type()->indexId) && false !== $this->meta->type()->indexId)
56
		{
57 15
			$this->isIndexable = true;
58
		}
59 17
		if ($this->isIndexable)
60
		{
61 15
			if (!isset($this->model->_id))
62
			{
63
				throw new ManganelException(sprintf('Property `_id` is not set in model `%s`, this is required by Manganel', get_class($this->model)));
64
			}
65 15
			$this->manganel = Manganel::create($this->model);
66
		}
67 17
	}
68
69 17
	public function index()
70
	{
71 17
		if (!$this->isIndexable)
72
		{
73 3
			return;
74
		}
75
		// NOTE: Transformer must ensure that _id is string, not MongoId
76 15
		$body = SearchArray::fromModel($this->model);
77 15
		if (array_key_exists('_id', $body))
78
		{
79
			$config = Mangan::fromModel($this->model)->sanitizersMap;
80
			if (!array_key_exists(SearchArray::class, $config))
81
			{
82
				throw new UnexpectedValueException(sprintf('Mangan is not properly configured for Manganel. Signals must be generated or add configuration manually from `%s::getDefault()`', ConfigManager::class));
83
			}
84
			else
85
			{
86
				throw new UnexpectedValueException(sprintf('Cannot index `%s`, as it contains _id field. Either use MongoObjectId sanitizer on it, or rename.', get_class($this->model)));
87
			}
88
		}
89
		$params = [
90 15
			'body' => $body
91
		];
92 15
		$this->getClient()->index($this->getParams($params));
93 15
	}
94
95 4
	public function delete()
96
	{
97 4
		if (!$this->isIndexable)
98
		{
99
			return;
100
		}
101 4
		$this->getClient()->delete($this->getParams());
102 4
	}
103
104 3
	public function get($id = null)
105
	{
106 3
		if (!$this->isIndexable)
107
		{
108
			return;
109
		}
110 3
		$params = $id ? ['id' => (string) $id] : [];
111 3
		$data = $this->getClient()->get($this->getParams($params))['_source'];
112 3
		return SearchArray::toModel($data);
113
	}
114
115
	/**
116
	 * Get client
117
	 * @return Client
118
	 */
119 15
	public function getClient()
120
	{
121 15
		return $this->manganel->getClient();
122
	}
123
124 15
	private function getParams($params = [])
125
	{
126
		$result = [
127 15
			'index' => strtolower($this->manganel->index),
128 15
			'type' => CollectionNamer::nameCollection($this->model),
129 15
			'id' => (string) $this->model->_id
130
		];
131 15
		return array_merge($result, $params);
132
	}
133
134
}
135