Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
25 | class Repository |
||
26 | { |
||
27 | /** |
||
28 | * @var Manager |
||
29 | */ |
||
30 | private $manager; |
||
31 | |||
32 | /** |
||
33 | * @var string Fully qualified class name |
||
34 | */ |
||
35 | private $className; |
||
36 | |||
37 | /** |
||
38 | * @var string Elasticsearch type name |
||
39 | */ |
||
40 | private $type; |
||
41 | |||
42 | /** |
||
43 | * Constructor. |
||
44 | * |
||
45 | * @param Manager $manager |
||
46 | * @param string $className |
||
47 | */ |
||
48 | public function __construct($manager, $className) |
||
49 | { |
||
50 | if (!is_string($className)) { |
||
51 | throw new \InvalidArgumentException('Class name must be a string.'); |
||
52 | } |
||
53 | |||
54 | if (!class_exists($className)) { |
||
55 | throw new \InvalidArgumentException( |
||
56 | sprintf('Cannot create repository for non-existing class "%s".', $className) |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | $this->manager = $manager; |
||
61 | $this->className = $className; |
||
62 | $this->type = $this->resolveType($className); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Returns elasticsearch manager used in the repository. |
||
67 | * |
||
68 | * @return Manager |
||
69 | */ |
||
70 | public function getManager() |
||
74 | |||
75 | /** |
||
76 | * @return array |
||
77 | */ |
||
78 | public function getType() |
||
82 | |||
83 | /** |
||
84 | * Returns a single document data by ID or null if document is not found. |
||
85 | * |
||
86 | * @param string $id Document ID to find |
||
87 | * @param string $routing Custom routing for the document |
||
88 | * |
||
89 | * @return object |
||
90 | */ |
||
91 | public function find($id, $routing = null) |
||
92 | { |
||
93 | return $this->manager->find($this->type, $id, $routing); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns documents by a set of ids |
||
98 | * |
||
99 | * @param array $ids |
||
100 | * |
||
101 | * @return DocumentIterator The objects. |
||
102 | */ |
||
103 | public function findByIds(array $ids) |
||
104 | { |
||
105 | $args = []; |
||
106 | $manager = $this->getManager(); |
||
107 | $args['body']['docs'] = []; |
||
108 | $args['index'] = $manager->getIndexName(); |
||
109 | $args['type'] = $this->getType(); |
||
110 | |||
111 | foreach ($ids as $id) { |
||
112 | $args['body']['docs'][] = [ |
||
113 | '_id' => $id |
||
114 | ]; |
||
115 | } |
||
116 | |||
117 | $mgetResponse = $manager->getClient()->mget($args); |
||
118 | |||
119 | $return = []; |
||
120 | |||
121 | foreach ($mgetResponse['docs'] as $item) { |
||
122 | if ($item['found']) { |
||
123 | $return['hits']['hits'][] = $item; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | $return['hits']['total'] = count($return['hits']['hits']); |
||
128 | |||
129 | return new DocumentIterator($return, $manager); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Finds documents by a set of criteria. |
||
134 | * |
||
135 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
136 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
137 | * @param int|null $limit Example: 5. |
||
138 | * @param int|null $offset Example: 30. |
||
139 | * |
||
140 | * @return array|DocumentIterator The objects. |
||
141 | */ |
||
142 | public function findBy( |
||
169 | |||
170 | /** |
||
171 | * Finds a single document by a set of criteria. |
||
172 | * |
||
173 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
174 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
175 | * |
||
176 | * @return object|null The object. |
||
177 | */ |
||
178 | public function findOneBy(array $criteria, array $orderBy = []) |
||
184 | |||
185 | /** |
||
186 | * Returns search instance. |
||
187 | * |
||
188 | * @return Search |
||
189 | */ |
||
190 | public function createSearch() |
||
194 | |||
195 | /** |
||
196 | * Executes given search. |
||
197 | * |
||
198 | * @deprecated Use strict execute functions instead. e.g. executeIterator, executeRawIterator. |
||
199 | * @param Search $search |
||
200 | * @param string $resultsType |
||
201 | * |
||
202 | * @return DocumentIterator|RawIterator|array |
||
203 | * |
||
204 | * @throws \Exception |
||
205 | */ |
||
206 | public function execute(Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
210 | |||
211 | |||
212 | /** |
||
213 | * Parses scroll configuration from raw response. |
||
214 | * |
||
215 | * @param array $raw |
||
216 | * @param string $scrollDuration |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | private function getScrollConfiguration($raw, $scrollDuration) |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Returns DocumentIterator with composed Document objects from array response. |
||
234 | * |
||
235 | * @deprecated Miss type in the function name, use findDocuments() instead. Will remove in 3.0 |
||
236 | * |
||
237 | * @param Search $search |
||
238 | * @return DocumentIterator |
||
239 | */ |
||
240 | public function findDocument(Search $search) |
||
244 | |||
245 | |||
246 | /** |
||
247 | * Returns DocumentIterator with composed Document objects from array response. |
||
248 | * |
||
249 | * @param Search $search |
||
250 | * |
||
251 | * @return DocumentIterator |
||
252 | */ |
||
253 | View Code Duplication | public function findDocuments(Search $search) |
|
263 | |||
264 | |||
265 | /** |
||
266 | * Returns ArrayIterator with access to unmodified documents directly. |
||
267 | * |
||
268 | * @param Search $search |
||
269 | * |
||
270 | * @return ArrayIterator |
||
271 | */ |
||
272 | View Code Duplication | public function findArray(Search $search) |
|
282 | |||
283 | /** |
||
284 | * Returns RawIterator with access to node with all returned values included. |
||
285 | * |
||
286 | * @param Search $search |
||
287 | * |
||
288 | * @return RawIterator |
||
289 | */ |
||
290 | View Code Duplication | public function findRaw(Search $search) |
|
300 | |||
301 | /** |
||
302 | * Executes search to the elasticsearch and returns raw response. |
||
303 | * |
||
304 | * @param Search $search |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | private function executeSearch(Search $search) |
||
312 | |||
313 | /** |
||
314 | * Counts documents by given search. |
||
315 | * |
||
316 | * @param Search $search |
||
317 | * @param array $params |
||
318 | * @param bool $returnRaw If set true returns raw response gotten from client. |
||
319 | * |
||
320 | * @return int|array |
||
321 | */ |
||
322 | public function count(Search $search, array $params = [], $returnRaw = false) |
||
343 | |||
344 | /** |
||
345 | * Removes a single document data by ID. |
||
346 | * |
||
347 | * @param string $id Document ID to remove |
||
348 | * @param string $routing Custom routing for the document |
||
349 | * |
||
350 | * @return array |
||
351 | * |
||
352 | * @throws \LogicException |
||
353 | */ |
||
354 | public function remove($id, $routing = null) |
||
370 | |||
371 | /** |
||
372 | * Partial document update. |
||
373 | * |
||
374 | * @param string $id Document id to update. |
||
375 | * @param array $fields Fields array to update. |
||
376 | * @param string $script Groovy script to update fields. |
||
377 | * @param array $params Additional parameters to pass to the client. |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | public function update($id, array $fields = [], $script = null, array $params = []) |
||
402 | |||
403 | /** |
||
404 | * Resolves elasticsearch type by class name. |
||
405 | * |
||
406 | * @param string $className |
||
407 | * |
||
408 | * @return array |
||
409 | */ |
||
410 | private function resolveType($className) |
||
414 | |||
415 | /** |
||
416 | * Returns fully qualified class name. |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | public function getClassName() |
||
424 | } |
||
425 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.