1 | <?php |
||
31 | class IndexManager |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Manganel instance |
||
36 | * @var Manganel |
||
37 | */ |
||
38 | private $manganel = null; |
||
39 | |||
40 | /** |
||
41 | * Model meta data |
||
42 | * @var ManganelMeta |
||
43 | */ |
||
44 | private $meta = null; |
||
45 | |||
46 | /** |
||
47 | * Annotated model |
||
48 | * @var AnnotatedInterface |
||
49 | */ |
||
50 | private $model; |
||
51 | |||
52 | /** |
||
53 | * Whether model is indexable |
||
54 | * @var bool |
||
55 | */ |
||
56 | private $isIndexable = false; |
||
57 | |||
58 | 21 | public function __construct($model) |
|
59 | { |
||
60 | 21 | $this->model = $model; |
|
61 | 21 | $this->meta = ManganelMeta::create($this->model); |
|
62 | 21 | if (!empty($this->meta->type()->indexId) && false !== $this->meta->type()->indexId) |
|
63 | 21 | { |
|
64 | 19 | $this->isIndexable = true; |
|
65 | 19 | } |
|
66 | 21 | if ($this->isIndexable) |
|
67 | 21 | { |
|
68 | 19 | if (!isset($this->model->_id)) |
|
69 | 19 | { |
|
70 | throw new ManganelException(sprintf('Property `_id` is not set in model `%s`, this is required by Manganel', get_class($this->model))); |
||
71 | } |
||
72 | 19 | $this->manganel = Manganel::create($this->model); |
|
73 | 19 | } |
|
74 | 21 | } |
|
75 | |||
76 | 21 | public function index() |
|
77 | { |
||
78 | 21 | if (!$this->isIndexable) |
|
79 | 21 | { |
|
80 | 3 | return; |
|
81 | } |
||
82 | // NOTE: Transformer must ensure that _id is string, not MongoId |
||
83 | 19 | $body = SearchArray::fromModel($this->model); |
|
84 | 19 | if (array_key_exists('_id', $body)) |
|
85 | 19 | { |
|
86 | $config = Mangan::fromModel($this->model)->sanitizersMap; |
||
87 | if (!array_key_exists(SearchArray::class, $config)) |
||
88 | { |
||
89 | throw new UnexpectedValueException(sprintf('Mangan is not properly configured for Manganel. Signals must be generated or add configuration manually from `%s::getDefault()`', ConfigManager::class)); |
||
90 | } |
||
91 | else |
||
92 | { |
||
93 | throw new UnexpectedValueException(sprintf('Cannot index `%s`, as it contains _id field. Either use MongoObjectId sanitizer on it, or rename.', get_class($this->model))); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | // In some cases $value *might* still be mongoId type, |
||
98 | // see https://github.com/Maslosoft/Addendum/issues/43 |
||
99 | 19 | $func = function($value) |
|
100 | { |
||
101 | 19 | if ($value instanceof MongoId) |
|
102 | 19 | { |
|
103 | return (string) $value; |
||
104 | } |
||
105 | 19 | return $value; |
|
106 | 19 | }; |
|
107 | 19 | $filtered = filter_var($body, \FILTER_CALLBACK, ['options' => $func]); |
|
108 | |||
109 | // Create proper elastic search request array |
||
110 | $params = [ |
||
111 | 19 | 'body' => $filtered, |
|
112 | 19 | ]; |
|
113 | try |
||
114 | { |
||
115 | 19 | $this->getClient()->index($this->getParams($params)); |
|
116 | } |
||
117 | 19 | catch (BadRequest400Exception $e) |
|
118 | { |
||
119 | // Throw previous exception, |
||
120 | // as it holds more meaningfull information |
||
121 | $previous = $e->getPrevious(); |
||
122 | $message = sprintf('Exception while indexing `%s`@`%s`: %s', get_class($this->model), $this->manganel->indexId, $previous->getMessage()); |
||
123 | throw new BadRequest400Exception($message); |
||
124 | } |
||
125 | 19 | } |
|
126 | |||
127 | 4 | public function delete() |
|
128 | { |
||
129 | 4 | if (!$this->isIndexable) |
|
130 | 4 | { |
|
131 | return; |
||
132 | } |
||
133 | 4 | $this->getClient()->delete($this->getParams()); |
|
134 | 4 | } |
|
135 | |||
136 | 4 | public function get($id = null) |
|
137 | { |
||
138 | 4 | if (!$this->isIndexable) |
|
139 | 4 | { |
|
140 | return; |
||
141 | } |
||
142 | 4 | $params = $id ? ['id' => (string) $id] : []; |
|
143 | 4 | $data = $this->getClient()->get($this->getParams($params))['_source']; |
|
144 | 4 | return SearchArray::toModel($data); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Get client |
||
149 | * @return Client |
||
150 | */ |
||
151 | 19 | public function getClient() |
|
155 | |||
156 | 19 | private function getParams($params = []) |
|
157 | { |
||
158 | // Check refresh option |
||
159 | 19 | if ($this->manganel->refresh instanceof Closure) |
|
176 | |||
177 | } |
||
178 |