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 | * Initiates a new AlgoliaManager. |
||
78 | * |
||
79 | * @param Client $client |
||
80 | * @param ActiveRecordFactory $activeRecordFactory |
||
81 | * @param ActiveQueryChunker $activeQueryChunker |
||
82 | */ |
||
83 | 56 | public function __construct( |
|
84 | Client $client, |
||
85 | ActiveRecordFactory $activeRecordFactory, |
||
86 | ActiveQueryChunker $activeQueryChunker |
||
87 | ) { |
||
88 | 56 | $this->client = $client; |
|
89 | 56 | $this->activeRecordFactory = $activeRecordFactory; |
|
90 | 56 | $this->activeQueryChunker = $activeQueryChunker; |
|
91 | 56 | } |
|
92 | |||
93 | /** |
||
94 | * Returns the Algolia Client. |
||
95 | * |
||
96 | * @return Client |
||
97 | */ |
||
98 | 23 | public function getClient() |
|
99 | { |
||
100 | 23 | return $this->client; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Sets the environment for the manager. |
||
105 | * |
||
106 | * @param string $env |
||
107 | */ |
||
108 | 56 | public function setEnv($env) |
|
109 | { |
||
110 | 56 | $this->env = $env; |
|
111 | 56 | } |
|
112 | |||
113 | /** |
||
114 | * Returns the environment for the manager. |
||
115 | * |
||
116 | * @return null|string |
||
117 | */ |
||
118 | 1 | public function getEnv() |
|
119 | { |
||
120 | 1 | return $this->env; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Indexes a searchable model to all indices. |
||
125 | * |
||
126 | * @param SearchableInterface $searchableModel |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 8 | public function pushToIndices(SearchableInterface $searchableModel) |
|
131 | { |
||
132 | 8 | $indices = $this->initIndices($searchableModel); |
|
133 | 8 | $response = []; |
|
134 | |||
135 | 8 | foreach ($indices as $index) { |
|
136 | 8 | $record = $searchableModel->getAlgoliaRecord(); |
|
137 | 8 | $response[$index->indexName] = $index->addObject($record, $searchableModel->getObjectID()); |
|
138 | 8 | } |
|
139 | |||
140 | 8 | return $response; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Indexes multiple searchable models in a batch. The given searchable models must be of the same class. |
||
145 | * |
||
146 | * @param SearchableInterface[] $searchableModels |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | 5 | public function pushMultipleToIndices(array $searchableModels) |
|
151 | { |
||
152 | 5 | $algoliaRecords = $this->getAlgoliaRecordsFromSearchableModelArray($searchableModels); |
|
153 | 2 | $indices = $this->initIndices($searchableModels[0]); |
|
154 | |||
155 | 2 | $response = []; |
|
156 | |||
157 | 2 | foreach ($indices as $index) { |
|
158 | /* @var Index $index */ |
||
159 | 2 | $response[$index->indexName] = $index->addObjects($algoliaRecords); |
|
160 | 2 | } |
|
161 | |||
162 | 2 | return $response; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Updates the models data in all indices. |
||
167 | * |
||
168 | * @param SearchableInterface $searchableModel |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | 3 | public function updateInIndices(SearchableInterface $searchableModel) |
|
173 | { |
||
174 | 3 | $indices = $this->initIndices($searchableModel); |
|
175 | 3 | $response = []; |
|
176 | |||
177 | 3 | foreach ($indices as $index) { |
|
178 | 3 | $record = $searchableModel->getAlgoliaRecord(); |
|
179 | 3 | $record['objectID'] = $searchableModel->getObjectID(); |
|
180 | 3 | $response[$index->indexName] = $index->saveObject($record); |
|
181 | 3 | } |
|
182 | |||
183 | 3 | return $response; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Updates multiple models data in all indices. The given searchable models must be of the same class. |
||
188 | * |
||
189 | * @param SearchableInterface[] $searchableModels |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | 4 | public function updateMultipleInIndices(array $searchableModels) |
|
194 | { |
||
195 | 4 | $algoliaRecords = $this->getAlgoliaRecordsFromSearchableModelArray($searchableModels); |
|
196 | 2 | $indices = $this->initIndices($searchableModels[0]); |
|
197 | |||
198 | 2 | $response = []; |
|
199 | |||
200 | 2 | foreach ($indices as $index) { |
|
201 | /* @var Index $index */ |
||
202 | 2 | $response[$index->indexName] = $index->saveObjects($algoliaRecords); |
|
203 | 2 | } |
|
204 | |||
205 | 2 | return $response; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Removes a searchable model from indices. |
||
210 | * |
||
211 | * @param SearchableInterface $searchableModel |
||
212 | * |
||
213 | * @return array |
||
214 | * @throws \InvalidArgumentException |
||
215 | */ |
||
216 | 3 | public function removeFromIndices(SearchableInterface $searchableModel) |
|
217 | { |
||
218 | 3 | $indices = $indices = $this->initIndices($searchableModel); |
|
219 | 3 | $response = []; |
|
220 | |||
221 | 3 | foreach ($indices as $index) { |
|
222 | 3 | $objectID = $searchableModel->getObjectID(); |
|
223 | 3 | $response[$index->indexName] = $index->deleteObject($objectID); |
|
224 | 3 | } |
|
225 | |||
226 | 3 | return $response; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Removes multiple models from all indices. The given searchable models must be of the same class. |
||
231 | * |
||
232 | * @param array $searchableModels |
||
233 | * |
||
234 | * @return array |
||
235 | * @throws \InvalidArgumentException |
||
236 | */ |
||
237 | 5 | public function removeMultipleFromIndices(array $searchableModels) |
|
254 | |||
255 | /** |
||
256 | * Re-indexes the indices safely for the given ActiveRecord Class. |
||
257 | * |
||
258 | * @param string $className The name of the ActiveRecord to be indexed |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | 3 | public function reindex($className) |
|
263 | { |
||
285 | |||
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 | 3 | public function reindexOnly(array $searchableModels) |
|
308 | |||
309 | /** |
||
310 | * Re-indexes the related indices for the given ActiveQueryInterface. |
||
311 | * The result of the given ActiveQuery must consist from Searchable models of the same class. |
||
312 | * |
||
313 | * @param ActiveQueryInterface $activeQuery |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | 4 | public function reindexByActiveQuery(ActiveQueryInterface $activeQuery) |
|
345 | |||
346 | /** |
||
347 | * Clears the indices for the given Class that implements SearchableInterface. |
||
348 | * |
||
349 | * @param string $className The name of the Class which indices are to be cleared. |
||
350 | * |
||
351 | * @throws \InvalidArgumentException |
||
352 | * @return array |
||
353 | */ |
||
354 | 2 | public function clearIndices($className) |
|
369 | |||
370 | /** |
||
371 | * @param string $className The name of the class which is to be searched. |
||
372 | * @param string $query |
||
373 | * @param array $searchParameters Optional search parameters given as an associative array. |
||
|
|||
374 | * |
||
375 | * @link https://github.com/algolia/algoliasearch-client-php#search-parameters Allowed search parameters. |
||
376 | * |
||
377 | * @return array |
||
378 | 20 | */ |
|
379 | public function search($className, $query, array $searchParameters = null) |
||
394 | |||
395 | 16 | /** |
|
396 | * Dynamically pass methods to the Algolia Client. |
||
397 | * |
||
398 | * @param string $method |
||
399 | * @param array $parameters |
||
400 | * |
||
401 | * @return mixed |
||
402 | */ |
||
403 | public function __call($method, $parameters) |
||
407 | |||
408 | /** |
||
409 | 19 | * Checks if the given class implements SearchableInterface. |
|
410 | 3 | * |
|
411 | 3 | * @param string $class Either name or instance of the class to be checked. |
|
412 | */ |
||
413 | 19 | private function checkImplementsSearchableInterface($class) |
|
421 | |||
422 | /** |
||
423 | * Initializes indices for the given SearchableModel. |
||
424 | * |
||
425 | * @param SearchableInterface $searchableModel |
||
426 | 23 | * |
|
427 | * @return Index[] |
||
428 | 23 | */ |
|
429 | 4 | private function initIndices(SearchableInterface $searchableModel) |
|
443 | |||
444 | 15 | /** |
|
445 | 15 | * Maps an array of searchable models into an Algolia friendly array. |
|
446 | * |
||
447 | 10 | * @param SearchableInterface[] $searchableModels |
|
448 | * |
||
449 | * @return array |
||
450 | */ |
||
451 | private function getAlgoliaRecordsFromSearchableModelArray(array $searchableModels) |
||
474 | |||
475 | /** |
||
476 | * Reindex atomically the given index with the given records. |
||
477 | * |
||
478 | * @param Index $index |
||
479 | * @param array $algoliaRecords |
||
480 | * |
||
481 | * @return mixed |
||
482 | */ |
||
483 | private function reindexAtomically(Index $index, array $algoliaRecords) |
||
498 | |||
499 | } |
||
500 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.