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 |
||
26 | class ObjectPersister implements ObjectPersisterInterface |
||
27 | { |
||
28 | protected $type; |
||
29 | protected $transformer; |
||
30 | protected $objectClass; |
||
31 | protected $fields; |
||
32 | protected $logger; |
||
33 | private $options; |
||
34 | |||
35 | /** |
||
36 | * @param Type $type |
||
37 | * @param ModelToElasticaTransformerInterface $transformer |
||
38 | * @param string $objectClass |
||
39 | * @param array $fields |
||
40 | */ |
||
41 | public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields, array $options = []) |
||
42 | { |
||
43 | $this->type = $type; |
||
44 | $this->transformer = $transformer; |
||
45 | $this->objectClass = $objectClass; |
||
46 | $this->fields = $fields; |
||
47 | $this->options = $options; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function handlesObject($object) |
||
57 | |||
58 | /** |
||
59 | * @param LoggerInterface $logger |
||
60 | */ |
||
61 | public function setLogger(LoggerInterface $logger) |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function insertOne($object) |
||
70 | { |
||
71 | $this->insertMany([$object]); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function replaceOne($object) |
||
78 | { |
||
79 | $this->replaceMany([$object]); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function deleteOne($object) |
||
86 | { |
||
87 | $this->deleteMany([$object]); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function deleteById($id, $routing = false) |
||
94 | { |
||
95 | $this->deleteManyByIdentifiers([$id], $routing); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | View Code Duplication | public function insertMany(array $objects) |
|
102 | { |
||
103 | $documents = []; |
||
104 | foreach ($objects as $object) { |
||
105 | $documents[] = $this->transformToElasticaDocument($object); |
||
106 | } |
||
107 | try { |
||
108 | $this->type->addDocuments($documents, $this->options); |
||
109 | } catch (BulkException $e) { |
||
110 | $this->log($e); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function replaceMany(array $objects) |
||
118 | { |
||
119 | $documents = []; |
||
120 | foreach ($objects as $object) { |
||
121 | $document = $this->transformToElasticaDocument($object); |
||
122 | $document->setDocAsUpsert(true); |
||
123 | $documents[] = $document; |
||
124 | } |
||
125 | |||
126 | try { |
||
127 | $this->type->updateDocuments($documents, $this->options); |
||
128 | } catch (BulkException $e) { |
||
129 | $this->log($e); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | View Code Duplication | public function deleteMany(array $objects) |
|
137 | { |
||
138 | $documents = []; |
||
139 | foreach ($objects as $object) { |
||
140 | $documents[] = $this->transformToElasticaDocument($object); |
||
141 | } |
||
142 | try { |
||
143 | $this->type->deleteDocuments($documents); |
||
144 | } catch (BulkException $e) { |
||
145 | $this->log($e); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function deleteManyByIdentifiers(array $identifiers, $routing = false) |
||
153 | { |
||
154 | try { |
||
155 | $this->type->deleteIds($identifiers, $routing); |
||
156 | } catch (BulkException $e) { |
||
157 | $this->log($e); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Transforms an object to an elastica document. |
||
163 | * |
||
164 | * @param object $object |
||
165 | * |
||
166 | * @return Document the elastica document |
||
167 | */ |
||
168 | public function transformToElasticaDocument($object) |
||
169 | { |
||
170 | return $this->transformer->transform($object, $this->fields); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
||
175 | * |
||
176 | * @param BulkException $e |
||
177 | * |
||
178 | * @throws BulkException |
||
179 | */ |
||
180 | private function log(BulkException $e) |
||
188 | } |
||
189 |