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 | |||
34 | /** |
||
35 | * @param Type $type |
||
36 | * @param ModelToElasticaTransformerInterface $transformer |
||
37 | * @param string $objectClass |
||
38 | * @param array $fields |
||
39 | */ |
||
40 | 17 | public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields) |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function handlesObject($object) |
||
55 | |||
56 | /** |
||
57 | * @param LoggerInterface $logger |
||
58 | */ |
||
59 | public function setLogger(LoggerInterface $logger) |
||
63 | |||
64 | /** |
||
65 | * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
||
66 | * |
||
67 | * @param BulkException $e |
||
68 | * |
||
69 | * @throws BulkException |
||
70 | */ |
||
71 | private function log(BulkException $e) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 7 | public function insertOne($object) |
|
84 | { |
||
85 | 7 | return $this->insertMany([$object]); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 5 | public function replaceOne($object) |
|
92 | { |
||
93 | 5 | return $this->replaceMany([$object]); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 3 | public function deleteOne($object) |
|
100 | { |
||
101 | 3 | return $this->deleteMany([$object]); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function deleteById($id) |
||
108 | { |
||
109 | return $this->deleteManyByIdentifiers([$id]); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 10 | View Code Duplication | public function insertMany(array $objects) |
116 | { |
||
117 | 10 | $documents = []; |
|
118 | 10 | foreach ($objects as $object) { |
|
119 | 10 | $documents[] = $this->transformToElasticaDocument($object); |
|
120 | } |
||
121 | try { |
||
122 | 8 | return $this->type->addDocuments($documents); |
|
123 | } catch (BulkException $e) { |
||
124 | $this->log($e); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 5 | public function replaceMany(array $objects) |
|
132 | { |
||
133 | 5 | $documents = []; |
|
134 | 5 | foreach ($objects as $object) { |
|
135 | 5 | $document = $this->transformToElasticaDocument($object); |
|
136 | 4 | $document->setDocAsUpsert(true); |
|
137 | 4 | $documents[] = $document; |
|
138 | } |
||
139 | |||
140 | try { |
||
141 | 4 | return $this->type->updateDocuments($documents); |
|
142 | } catch (BulkException $e) { |
||
143 | $this->log($e); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 3 | View Code Duplication | public function deleteMany(array $objects) |
151 | { |
||
152 | 3 | $documents = []; |
|
153 | 3 | foreach ($objects as $object) { |
|
154 | 3 | $documents[] = $this->transformToElasticaDocument($object); |
|
155 | } |
||
156 | try { |
||
157 | 2 | return $this->type->deleteDocuments($documents); |
|
158 | } catch (BulkException $e) { |
||
159 | $this->log($e); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function deleteManyByIdentifiers(array $identifiers) |
||
174 | |||
175 | /** |
||
176 | * Transforms an object to an elastica document. |
||
177 | * |
||
178 | * @param object $object |
||
179 | * |
||
180 | * @return Document the elastica document |
||
181 | */ |
||
182 | 6 | public function transformToElasticaDocument($object) |
|
186 | } |
||
187 |