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 | 55 | public function __construct($model) |
|
75 | |||
76 | 55 | public function index() |
|
77 | { |
||
78 | 55 | if (!$this->isIndexable) |
|
79 | { |
||
80 | 3 | return; |
|
81 | } |
||
82 | // NOTE: Transformer must ensure that _id is string, not MongoId |
||
83 | 53 | $body = SearchArray::fromModel($this->model); |
|
84 | 53 | if (array_key_exists('_id', $body)) |
|
85 | { |
||
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 | // Create proper elastic search request array |
||
98 | $params = [ |
||
99 | 53 | 'body' => RecursiveFilter::mongoIdToString($body), |
|
100 | ]; |
||
101 | try |
||
102 | { |
||
103 | 53 | $fullParams = $this->getParams($params); |
|
104 | // Need to check if exists, or update will fail |
||
105 | $existsParams = [ |
||
106 | 53 | 'index' => $fullParams['index'], |
|
107 | 53 | 'type' => $fullParams['type'], |
|
108 | 53 | 'id' => $fullParams['id'] |
|
109 | ]; |
||
110 | 53 | $exists = $this->getClient()->exists($existsParams); |
|
111 | |||
112 | 53 | if (!$exists) |
|
113 | { |
||
114 | 53 | $result = $this->getClient()->index($fullParams); |
|
115 | } |
||
116 | else |
||
117 | { |
||
118 | 13 | $updateParams = $fullParams; |
|
119 | 13 | $updateParams['body'] = [ |
|
120 | 13 | 'doc' => $fullParams['body'] |
|
121 | ]; |
||
122 | 13 | $result = $this->getClient()->update($updateParams); |
|
123 | } |
||
124 | 53 | if (array_key_exists('result', $result) && $result['result'] === 'updated') |
|
125 | { |
||
126 | // For ES 5 |
||
127 | 3 | return true; |
|
128 | } |
||
129 | 53 | elseif (is_array($result)) |
|
130 | { |
||
131 | // For earlier ES |
||
132 | 53 | return true; |
|
133 | } |
||
134 | } |
||
135 | catch (BadRequest400Exception $e) |
||
136 | { |
||
137 | // Throw previous exception, |
||
138 | // as it holds more meaningfull information |
||
139 | $previous = $e->getPrevious(); |
||
140 | $message = sprintf('Exception while indexing `%s`@`%s`: %s', get_class($this->model), $this->manganel->indexId, $previous->getMessage()); |
||
141 | throw new BadRequest400Exception($message, 400, $e); |
||
142 | } |
||
143 | return false; |
||
144 | } |
||
145 | |||
146 | 5 | public function delete() |
|
154 | |||
155 | 11 | public function get($id = null) |
|
165 | |||
166 | /** |
||
167 | * Get client |
||
168 | * @return Client |
||
169 | */ |
||
170 | 53 | public function getClient() |
|
174 | |||
175 | 53 | private function getParams($params = []) |
|
195 | |||
196 | } |
||
197 |