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 | * {@inheritdoc} |
||
66 | */ |
||
67 | 7 | public function insertOne($object) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 5 | public function replaceOne($object) |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 3 | public function deleteOne($object) |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function deleteById($id) |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 10 | View Code Duplication | public function insertMany(array $objects) |
100 | { |
||
101 | 10 | $documents = []; |
|
102 | 10 | foreach ($objects as $object) { |
|
103 | 10 | $documents[] = $this->transformToElasticaDocument($object); |
|
104 | 8 | } |
|
105 | try { |
||
106 | 8 | $this->type->addDocuments($documents); |
|
107 | 8 | } catch (BulkException $e) { |
|
108 | $this->log($e); |
||
109 | } |
||
110 | 8 | } |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 5 | public function replaceMany(array $objects) |
|
116 | { |
||
117 | 5 | $documents = []; |
|
118 | 5 | foreach ($objects as $object) { |
|
119 | 5 | $document = $this->transformToElasticaDocument($object); |
|
120 | 4 | $document->setDocAsUpsert(true); |
|
121 | 4 | $documents[] = $document; |
|
122 | 4 | } |
|
123 | |||
124 | try { |
||
125 | 4 | $this->type->updateDocuments($documents); |
|
126 | 4 | } catch (BulkException $e) { |
|
127 | $this->log($e); |
||
128 | } |
||
129 | 4 | } |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 3 | View Code Duplication | public function deleteMany(array $objects) |
135 | { |
||
136 | 3 | $documents = []; |
|
137 | 3 | foreach ($objects as $object) { |
|
138 | 3 | $documents[] = $this->transformToElasticaDocument($object); |
|
139 | 2 | } |
|
140 | try { |
||
141 | 2 | $this->type->deleteDocuments($documents); |
|
142 | 2 | } catch (BulkException $e) { |
|
143 | $this->log($e); |
||
144 | } |
||
145 | 2 | } |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function deleteManyByIdentifiers(array $identifiers) |
||
158 | |||
159 | /** |
||
160 | * Transforms an object to an elastica document. |
||
161 | * |
||
162 | * @param object $object |
||
163 | * |
||
164 | * @return Document the elastica document |
||
165 | */ |
||
166 | 6 | public function transformToElasticaDocument($object) |
|
170 | |||
171 | /** |
||
172 | * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
||
173 | * |
||
174 | * @param BulkException $e |
||
175 | * |
||
176 | * @throws BulkException |
||
177 | */ |
||
178 | private function log(BulkException $e) |
||
186 | } |
||
187 |