|
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
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* IndexMangager |
|
20
|
|
|
* |
|
21
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
22
|
|
|
*/ |
|
23
|
|
|
class IndexManager |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Manganel instance |
|
28
|
|
|
* @var Manganel |
|
29
|
|
|
*/ |
|
30
|
|
|
private $manganel = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Model meta data |
|
34
|
|
|
* @var ManganelMeta |
|
35
|
|
|
*/ |
|
36
|
|
|
private $meta = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Annotated model |
|
40
|
|
|
* @var AnnotatedInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $model; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Whether model is indexable |
|
46
|
|
|
* @var bool |
|
47
|
|
|
*/ |
|
48
|
|
|
private $isIndexable = false; |
|
49
|
|
|
|
|
50
|
5 |
|
public function __construct($model) |
|
51
|
|
|
{ |
|
52
|
5 |
|
$this->model = $model; |
|
53
|
5 |
|
$this->meta = ManganelMeta::create($this->model); |
|
54
|
5 |
|
if (!empty($this->meta->type()->indexId) && false !== $this->meta->type()->indexId) |
|
55
|
5 |
|
{ |
|
56
|
3 |
|
$this->isIndexable = true; |
|
57
|
3 |
|
} |
|
58
|
5 |
|
if ($this->isIndexable) |
|
59
|
5 |
|
{ |
|
60
|
3 |
|
if (!isset($this->model->_id)) |
|
61
|
3 |
|
{ |
|
62
|
|
|
throw new ManganelException(sprintf('Property `_id` is not set in model `%s`, this is required by Manganel', get_class($this->model))); |
|
63
|
1 |
|
} |
|
64
|
3 |
|
$this->manganel = Manganel::create($this->model); |
|
65
|
3 |
|
} |
|
66
|
5 |
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
public function index() |
|
69
|
|
|
{ |
|
70
|
5 |
|
if (!$this->isIndexable) |
|
71
|
5 |
|
{ |
|
72
|
2 |
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
// NOTE: Transformer must ensure that _id is string, not MongoId |
|
75
|
|
|
$params = [ |
|
76
|
3 |
|
'body' => SearchArray::fromModel($this->model) |
|
77
|
3 |
|
]; |
|
78
|
3 |
|
$this->getClient()->index($this->getParams($params)); |
|
79
|
3 |
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function delete() |
|
82
|
|
|
{ |
|
83
|
1 |
|
if (!$this->isIndexable) |
|
84
|
1 |
|
{ |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
1 |
|
$this->getClient()->delete($this->getParams()); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function get($id = null) |
|
91
|
|
|
{ |
|
92
|
2 |
|
if (!$this->isIndexable) |
|
93
|
2 |
|
{ |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
2 |
|
$params = $id ? ['id' => (string) $id] : []; |
|
97
|
2 |
|
$data = $this->getClient()->get($this->getParams($params))['_source']; |
|
98
|
2 |
|
return SearchArray::toModel($data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get client |
|
103
|
|
|
* @return Client |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function getClient() |
|
106
|
|
|
{ |
|
107
|
3 |
|
return $this->manganel->getClient(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
private function getParams($params = []) |
|
111
|
|
|
{ |
|
112
|
|
|
$result = [ |
|
113
|
3 |
|
'index' => strtolower($this->manganel->index), |
|
114
|
3 |
|
'type' => CollectionNamer::nameCollection($this->model), |
|
115
|
3 |
|
'id' => (string) $this->model->_id |
|
116
|
3 |
|
]; |
|
117
|
3 |
|
return array_merge($result, $params); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|