1 | <?php |
||
37 | class AlgoliaManager |
||
38 | { |
||
39 | /** |
||
40 | * @var AlgoliaFactory |
||
41 | */ |
||
42 | protected $factory; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $config; |
||
48 | |||
49 | /** |
||
50 | * @var null|Client |
||
51 | */ |
||
52 | protected $client; |
||
53 | |||
54 | /** |
||
55 | * @var ActiveRecordFactory |
||
56 | */ |
||
57 | protected $activeRecordFactory; |
||
58 | |||
59 | /** |
||
60 | * @var null|string |
||
61 | */ |
||
62 | protected $env; |
||
63 | |||
64 | /** |
||
65 | * Initiates a new AlgoliaManager. |
||
66 | * |
||
67 | * @param AlgoliaFactory $algoliaFactory |
||
68 | * @param ActiveRecordFactory $activeRecordFactory |
||
69 | * @param array $config Configurations for the Algolia Client. |
||
70 | */ |
||
71 | 37 | public function __construct(AlgoliaFactory $algoliaFactory, ActiveRecordFactory $activeRecordFactory, array $config = []) |
|
77 | |||
78 | /** |
||
79 | * Returns the Algolia Client. |
||
80 | * |
||
81 | * @return Client |
||
82 | */ |
||
83 | 17 | public function getClient() |
|
91 | |||
92 | /** |
||
93 | * Returns the config array. |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | 2 | public function getConfig() |
|
101 | |||
102 | /** |
||
103 | * Sets the environment for the manager. |
||
104 | * |
||
105 | * @param string $env |
||
106 | */ |
||
107 | 37 | public function setEnv($env) |
|
111 | |||
112 | /** |
||
113 | * Returns the environment for the manager. |
||
114 | * |
||
115 | * @return null|string |
||
116 | */ |
||
117 | 1 | public function getEnv() |
|
118 | { |
||
119 | 1 | return $this->env; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Indexes a searchable model to all indices. |
||
124 | * |
||
125 | * @param SearchableInterface $searchableModel |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 5 | public function pushToIndices(SearchableInterface $searchableModel) |
|
141 | |||
142 | /** |
||
143 | * Indexes multiple searchable models in a batch. The given searchable models must be of the same class. |
||
144 | * |
||
145 | * @param SearchableInterface[] $searchableModels |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 3 | public function pushMultipleToIndices(array $searchableModels) |
|
161 | |||
162 | /** |
||
163 | * Updates the models data in all indices. |
||
164 | * |
||
165 | * @param SearchableInterface $searchableModel |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | 3 | public function updateInIndices(SearchableInterface $searchableModel) |
|
182 | |||
183 | /** |
||
184 | * Updates multiple models data in all indices. The given searchable models must be of the same class. |
||
185 | * |
||
186 | * @param SearchableInterface[] $searchableModels |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | 2 | public function updateMultipleInIndices(array $searchableModels) |
|
202 | |||
203 | /** |
||
204 | * Removes a searchable model from indices. |
||
205 | * |
||
206 | * @param SearchableInterface $searchableModel |
||
207 | * |
||
208 | * @return array |
||
209 | * @throws \Exception |
||
210 | */ |
||
211 | 2 | public function removeFromIndices(SearchableInterface $searchableModel) |
|
223 | |||
224 | /** |
||
225 | * Re-indexes the indices safely for the given ActiveRecord Class. |
||
226 | * |
||
227 | * @param string $className The name of the ActiveRecord to be indexed. |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | 3 | public function reindex($className) |
|
269 | |||
270 | /** |
||
271 | * Clears the indices for the given Class that implements SearchableInterface. |
||
272 | * |
||
273 | * @param string $className The name of the Class which indices are to be cleared. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | 2 | public function clearIndices($className) |
|
292 | |||
293 | /** |
||
294 | * Dynamically pass methods to the Algolia Client. |
||
295 | * |
||
296 | * @param string $method |
||
297 | * @param array $parameters |
||
298 | * |
||
299 | * @return mixed |
||
300 | */ |
||
301 | 16 | public function __call($method, $parameters) |
|
305 | |||
306 | /** |
||
307 | * Checks if the given class implements SearchableInterface. |
||
308 | * |
||
309 | * @param string $class Either name or instance of the class to be checked. |
||
310 | */ |
||
311 | 5 | private function checkImplementsSearchableInterface($class) |
|
319 | |||
320 | /** |
||
321 | * Returns the name of the class for given object. |
||
322 | * |
||
323 | * @param $class |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 5 | private function getClassName($class) |
|
333 | |||
334 | /** |
||
335 | * Initializes indices for the given SearchableModel. |
||
336 | * |
||
337 | * @param SearchableInterface $searchableModel |
||
338 | * |
||
339 | * @return Index[] |
||
340 | */ |
||
341 | 15 | private function initIndices(SearchableInterface $searchableModel) |
|
357 | |||
358 | /** |
||
359 | * Maps an array of searchable models into an Algolia friendly array. Returns also indices for the searchable model |
||
360 | * which the array consists of. |
||
361 | * |
||
362 | * @param SearchableInterface[] $searchableModels |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | 5 | private function getIndicesAndAlgoliaRecordsFromSearchableModelArray(array $searchableModels) |
|
387 | } |
||
388 |