1 | <?php |
||
39 | class AlgoliaManager |
||
40 | { |
||
41 | /** |
||
42 | * Size for the chunks used in reindexing methods |
||
43 | */ |
||
44 | const CHUNK_SIZE = 500; |
||
45 | |||
46 | /** |
||
47 | * @var AlgoliaFactory |
||
48 | */ |
||
49 | protected $factory; |
||
50 | |||
51 | /** |
||
52 | * @var AlgoliaConfig |
||
53 | */ |
||
54 | protected $config; |
||
55 | |||
56 | /** |
||
57 | * @var Client |
||
58 | */ |
||
59 | protected $client; |
||
60 | |||
61 | /** |
||
62 | * @var ActiveRecordFactory |
||
63 | */ |
||
64 | protected $activeRecordFactory; |
||
65 | |||
66 | /** |
||
67 | * @var null|string |
||
68 | */ |
||
69 | protected $env; |
||
70 | |||
71 | /** |
||
72 | * @var ActiveQueryChunker |
||
73 | */ |
||
74 | private $activeQueryChunker; |
||
75 | |||
76 | /** |
||
77 | 39 | * Initiates a new AlgoliaManager. |
|
78 | * |
||
79 | * @param Client $client |
||
80 | * @param ActiveRecordFactory $activeRecordFactory |
||
81 | * @param ActiveQueryChunker $activeQueryChunker |
||
82 | 39 | */ |
|
83 | 39 | public function __construct( |
|
92 | 17 | ||
93 | /** |
||
94 | 17 | * Returns the Algolia Client. |
|
95 | * |
||
96 | * @return Client |
||
97 | */ |
||
98 | public function getClient() |
||
102 | 39 | ||
103 | /** |
||
104 | 39 | * Sets the environment for the manager. |
|
105 | 39 | * |
|
106 | * @param string $env |
||
107 | */ |
||
108 | public function setEnv($env) |
||
112 | 1 | ||
113 | /** |
||
114 | 1 | * Returns the environment for the manager. |
|
115 | * |
||
116 | * @return null|string |
||
117 | */ |
||
118 | public function getEnv() |
||
122 | |||
123 | /** |
||
124 | 5 | * Indexes a searchable model to all indices. |
|
125 | * |
||
126 | 5 | * @param SearchableInterface $searchableModel |
|
127 | 5 | * |
|
128 | * @return array |
||
129 | 5 | */ |
|
130 | 5 | public function pushToIndices(SearchableInterface $searchableModel) |
|
142 | |||
143 | /** |
||
144 | 3 | * Indexes multiple searchable models in a batch. The given searchable models must be of the same class. |
|
145 | * |
||
146 | 3 | * @param SearchableInterface[] $searchableModels |
|
147 | 2 | * |
|
148 | * @return array |
||
149 | 2 | */ |
|
150 | public function pushMultipleToIndices(array $searchableModels) |
||
164 | |||
165 | /** |
||
166 | 3 | * Updates the models data in all indices. |
|
167 | * |
||
168 | 3 | * @param SearchableInterface $searchableModel |
|
169 | 3 | * |
|
170 | * @return array |
||
171 | 3 | */ |
|
172 | 3 | public function updateInIndices(SearchableInterface $searchableModel) |
|
185 | |||
186 | /** |
||
187 | 2 | * Updates multiple models data in all indices. The given searchable models must be of the same class. |
|
188 | * |
||
189 | 2 | * @param SearchableInterface[] $searchableModels |
|
190 | 1 | * |
|
191 | * @return array |
||
192 | 1 | */ |
|
193 | public function updateMultipleInIndices(array $searchableModels) |
||
207 | |||
208 | /** |
||
209 | * Removes a searchable model from indices. |
||
210 | 2 | * |
|
211 | * @param SearchableInterface $searchableModel |
||
212 | 2 | * |
|
213 | 2 | * @return array |
|
214 | * @throws \InvalidArgumentException |
||
215 | 2 | */ |
|
216 | 2 | public function removeFromIndices(SearchableInterface $searchableModel) |
|
228 | |||
229 | /** |
||
230 | 3 | * Removes multiple models from all indices. The given searchable models must be of the same class. |
|
231 | * |
||
232 | 3 | * @param array $searchableModels |
|
233 | 2 | * |
|
234 | 2 | * @return array |
|
235 | * @throws \InvalidArgumentException |
||
236 | 2 | */ |
|
237 | 2 | public function removeMultipleFromIndices(array $searchableModels) |
|
254 | |||
255 | /** |
||
256 | * Re-indexes the indices safely for the given ActiveRecord Class. |
||
257 | 2 | * |
|
258 | * @param string $className The name of the ActiveRecord to be indexed |
||
259 | 2 | * |
|
260 | 2 | * @return array |
|
261 | */ |
||
262 | 2 | public function reindex($className) |
|
285 | 1 | ||
286 | /** |
||
287 | * Re-indexes the related indices for the given array only with the objects from the given array. |
||
288 | * The given array must consist of Searchable objects of same class. |
||
289 | * |
||
290 | * @param SearchableInterface[] $searchableModels |
||
291 | * |
||
292 | * @throws \InvalidArgumentException |
||
293 | * @return array |
||
294 | */ |
||
295 | public function reindexOnly(array $searchableModels) |
||
308 | 5 | ||
309 | /** |
||
310 | 5 | * Re-indexes the related indices for the given ActiveQueryInterface. |
|
311 | 2 | * The result of the given ActiveQuery must consist from Searchable models of the same class. |
|
312 | * |
||
313 | 3 | * @param ActiveQueryInterface $activeQuery |
|
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | public function reindexByActiveQuery(ActiveQueryInterface $activeQuery) |
||
345 | |||
346 | /** |
||
347 | 7 | * Clears the indices for the given Class that implements SearchableInterface. |
|
348 | * |
||
349 | 7 | * @param string $className The name of the Class which indices are to be cleared. |
|
350 | 7 | * |
|
351 | 2 | * @throws \InvalidArgumentException |
|
352 | * @return array |
||
353 | */ |
||
354 | 7 | public function clearIndices($className) |
|
369 | |||
370 | /** |
||
371 | * Dynamically pass methods to the Algolia Client. |
||
372 | * |
||
373 | * @param string $method |
||
374 | * @param array $parameters |
||
375 | * |
||
376 | * @return mixed |
||
377 | */ |
||
378 | public function __call($method, $parameters) |
||
382 | |||
383 | /** |
||
384 | * Checks if the given class implements SearchableInterface. |
||
385 | * |
||
386 | * @param string $class Either name or instance of the class to be checked. |
||
387 | */ |
||
388 | private function checkImplementsSearchableInterface($class) |
||
396 | |||
397 | /** |
||
398 | * Initializes indices for the given SearchableModel. |
||
399 | * |
||
400 | * @param SearchableInterface $searchableModel |
||
401 | * |
||
402 | * @return Index[] |
||
403 | */ |
||
404 | private function initIndices(SearchableInterface $searchableModel) |
||
418 | |||
419 | /** |
||
420 | * Maps an array of searchable models into an Algolia friendly array. |
||
421 | * |
||
422 | * @param SearchableInterface[] $searchableModels |
||
423 | * |
||
424 | * @return array |
||
425 | */ |
||
426 | private function getAlgoliaRecordsFromSearchableModelArray(array $searchableModels) |
||
448 | |||
449 | /** |
||
450 | * Reindex atomically the given index with the given records. |
||
451 | * |
||
452 | * @param Index $index |
||
453 | * @param array $algoliaRecords |
||
454 | * |
||
455 | * @return mixed |
||
456 | */ |
||
457 | private function reindexAtomically(Index $index, array $algoliaRecords) |
||
472 | } |
||
473 |