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 |
||
30 | class DoctrineDbalRepository implements RepositoryInterface |
||
31 | { |
||
32 | use FilterUtilHelper; |
||
33 | |||
34 | /** |
||
35 | * @var Connection |
||
36 | */ |
||
37 | private $connection; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $tableName; |
||
43 | |||
44 | /** |
||
45 | * @var RateInterface[] |
||
46 | */ |
||
47 | private $identityMap = []; |
||
48 | |||
49 | /** |
||
50 | * DoctrineDbalRepository constructor. |
||
51 | * |
||
52 | * @param Connection $connection Dbal connection. |
||
53 | * @param string|null $tableName Table name in which rates will be stored, or NULL for 'runopencode_exchange_rate'. |
||
54 | */ |
||
55 | 10 | public function __construct(Connection $connection, $tableName = null) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 8 | public function save(array $rates) |
|
67 | { |
||
68 | 8 | $this->connection->beginTransaction(); |
|
69 | |||
70 | try { |
||
71 | |||
72 | /** |
||
73 | * @var RateInterface $rate |
||
74 | */ |
||
75 | 8 | foreach ($rates as $rate) { |
|
76 | |||
77 | 8 | if ($this->has($rate->getSourceName(), $rate->getCurrencyCode(), $rate->getDate(), $rate->getRateType())) { |
|
78 | |||
79 | 1 | $this->connection->executeQuery(sprintf('UPDATE %s SET rate_value = :rate_value, modified_at = :modified_at WHERE source_name = :source_name AND currency_code = :currency_code AND rate_date = :rate_date AND rate_type = :rate_type;', $this->tableName), [ |
|
80 | 1 | 'rate_value' => (float) $rate->getValue(), |
|
81 | 1 | 'source_name' => $rate->getSourceName(), |
|
82 | 1 | 'currency_code' => $rate->getCurrencyCode(), |
|
83 | 1 | 'rate_date' => $rate->getDate()->format('Y-m-d'), |
|
84 | 1 | 'rate_type' => $rate->getRateType(), |
|
85 | 1 | 'modified_at' => date('Y-m-d H:i:s'), |
|
86 | ]); |
||
87 | |||
88 | 1 | continue; |
|
89 | } |
||
90 | |||
91 | 8 | $this->connection->executeQuery(sprintf('INSERT INTO %s (source_name, rate_value, currency_code, rate_type, rate_date, base_currency_code, created_at, modified_at) VALUES (:source_name, :rate_value, :currency_code, :rate_type, :rate_date, :base_currency_code, :created_at, :modified_at);', $this->tableName), [ |
|
92 | 8 | 'source_name' => $rate->getSourceName(), |
|
93 | 8 | 'rate_value' => (float) $rate->getValue(), |
|
94 | 8 | 'currency_code' => $rate->getCurrencyCode(), |
|
95 | 8 | 'rate_type' => $rate->getRateType(), |
|
96 | 8 | 'rate_date' => $rate->getDate()->format('Y-m-d'), |
|
97 | 8 | 'base_currency_code' => $rate->getBaseCurrencyCode(), |
|
98 | 8 | 'created_at' => date('Y-m-d H:i:s'), |
|
99 | 8 | 'modified_at' => date('Y-m-d H:i:s'), |
|
100 | ]); |
||
101 | } |
||
102 | |||
103 | 7 | $this->connection->commit(); |
|
104 | 7 | $this->identityMap = []; |
|
105 | 1 | } catch (\Exception $e) { |
|
106 | 1 | $this->connection->rollBack(); |
|
107 | 1 | throw new ExchangeRateException('Unable to save rates.', 0, $e); |
|
108 | } |
||
109 | 7 | } |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 2 | public function delete(array $rates) |
|
115 | { |
||
116 | 2 | $this->connection->beginTransaction(); |
|
117 | |||
118 | try { |
||
119 | |||
120 | /** |
||
121 | * @var RateInterface $rate |
||
122 | */ |
||
123 | 2 | foreach ($rates as $rate) { |
|
124 | 2 | $key = $this->getRateKey($rate->getCurrencyCode(), $rate->getDate(), $rate->getRateType(), $rate->getSourceName()); |
|
125 | |||
126 | 2 | if (isset($this->identityMap[$key])) { |
|
127 | 1 | unset($this->identityMap[$key]); |
|
128 | } |
||
129 | |||
130 | 2 | $this->connection->executeQuery(sprintf('DELETE FROM %s WHERE source_name = :source_name AND currency_code = :currency_code AND rate_date = :rate_date AND rate_type = :rate_type;', $this->tableName), [ |
|
131 | 2 | 'source_name' => $rate->getSourceName(), |
|
132 | 2 | 'currency_code' => $rate->getCurrencyCode(), |
|
133 | 2 | 'rate_date' => $rate->getDate()->format('Y-m-d'), |
|
134 | 2 | 'rate_type' => $rate->getRateType(), |
|
135 | ]); |
||
136 | } |
||
137 | |||
138 | 1 | $this->connection->commit(); |
|
139 | 1 | } catch (\Exception $e) { |
|
140 | 1 | $this->connection->rollBack(); |
|
141 | 1 | throw new ExchangeRateException('Unable to delete rates.', 0, $e); |
|
142 | } |
||
143 | 1 | } |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 8 | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 8 | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
165 | { |
||
166 | 8 | if ($date === null) { |
|
167 | 3 | $date = new \DateTime('now'); |
|
168 | } |
||
169 | |||
170 | 8 | $key = $this->getRateKey($currencyCode, $date, $rateType, $sourceName); |
|
171 | |||
172 | 8 | if (!isset($this->identityMap[$key])) { |
|
173 | |||
174 | /** |
||
175 | * @var array $result |
||
176 | */ |
||
177 | 8 | $result = $this->connection->fetchAll( |
|
178 | 8 | sprintf('SELECT R.* FROM %s R WHERE R.source_name = :source_name AND R.currency_code = :currency_code AND R.rate_date = :rate_date AND R.rate_type = :rate_type;', $this->tableName), |
|
179 | [ |
||
180 | 8 | 'source_name' => $sourceName, |
|
181 | 8 | 'currency_code' => $currencyCode, |
|
182 | 8 | 'rate_date' => $date->format('Y-m-d'), |
|
183 | 8 | 'rate_type' => $rateType, |
|
184 | ] |
||
185 | ); |
||
186 | |||
187 | 8 | if (0 === count($result)) { |
|
188 | 8 | throw new ExchangeRateException(sprintf('Could not fetch rate for rate currency code "%s" and rate type "%s" on date "%s".', $currencyCode, $rateType, $date->format('Y-m-d'))); |
|
189 | } |
||
190 | |||
191 | 2 | $this->identityMap[$key] = $this->buildRateFromTableRowData($result[0]); |
|
192 | } |
||
193 | |||
194 | 2 | return $this->identityMap[$key]; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 2 | public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
201 | { |
||
202 | /** |
||
203 | * @var array $result |
||
204 | */ |
||
205 | 2 | $result = $this->connection->fetchAll( |
|
206 | 2 | sprintf('SELECT R.* FROM %s R WHERE R.source_name = :source_name AND R.currency_code = :currency_code AND R.rate_type = :rate_type ORDER BY R.rate_date DESC;', $this->tableName), |
|
207 | [ |
||
208 | 2 | 'source_name' => $sourceName, |
|
209 | 2 | 'currency_code' => $currencyCode, |
|
210 | 2 | 'rate_type' => $rateType, |
|
211 | ] |
||
212 | ); |
||
213 | |||
214 | 2 | if (0 === count($result)) { |
|
215 | 1 | throw new ExchangeRateException(sprintf('Could not fetch latest rate for rate currency code "%s" and rate type "%s".', $currencyCode, $rateType)); |
|
216 | } |
||
217 | |||
218 | 1 | $rate = $this->buildRateFromTableRowData($result[0]); |
|
219 | 1 | $key = $this->getRateKey($rate->getCurrencyCode(), $rate->getDate(), $rate->getRateType(), $rate->getSourceName()); |
|
|
|||
220 | |||
221 | 1 | return ($this->identityMap[$key] = $rate); |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | 3 | public function all(array $criteria = array()) |
|
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | 1 | public function count() |
|
311 | |||
312 | /** |
||
313 | * Builds rate key to speed up search. |
||
314 | * |
||
315 | * @param string $currencyCode |
||
316 | * @param \DateTime $date |
||
317 | * @param string $rateType |
||
318 | * @param string $sourceName |
||
319 | * @return string |
||
320 | */ |
||
321 | 9 | View Code Duplication | protected function getRateKey($currencyCode, $date, $rateType, $sourceName) |
329 | |||
330 | /** |
||
331 | * Initialize table schema where rates would be stored. |
||
332 | */ |
||
333 | 10 | protected function initialize() |
|
334 | { |
||
335 | 10 | if ($this->connection->getSchemaManager()->tablesExist([$this->tableName])) { |
|
336 | return; // @codeCoverageIgnore |
||
337 | } |
||
338 | |||
339 | 8 | $schema = new Schema(); |
|
340 | |||
341 | 8 | $table = $schema->createTable($this->tableName); |
|
342 | 8 | $table->addColumn('source_name', 'string', ['length' => 255]); |
|
343 | 8 | $table->addColumn('rate_value', 'float', ['precision' => 10, 'scale' => 4]); |
|
344 | 8 | $table->addColumn('currency_code', 'string', ['length' => 3]); |
|
345 | 8 | $table->addColumn('rate_type', 'string', ['length' => 255]); |
|
346 | 8 | $table->addColumn('rate_date', 'date', []); |
|
347 | 8 | $table->addColumn('base_currency_code', 'string', ['length' => 3]); |
|
348 | 8 | $table->addColumn('created_at', 'datetime', []); |
|
349 | 8 | $table->addColumn('modified_at', 'datetime', []); |
|
350 | |||
351 | 8 | $table->setPrimaryKey(['currency_code', 'rate_date', 'rate_type', 'source_name']); |
|
352 | |||
353 | 8 | $this->connection->exec($schema->toSql($this->connection->getDatabasePlatform())[0]); |
|
354 | 8 | } |
|
355 | |||
356 | /** |
||
357 | * Build rate from table row data. |
||
358 | * |
||
359 | * @param array $row Row data. |
||
360 | * @return Rate |
||
361 | */ |
||
362 | 6 | private function buildRateFromTableRowData(array $row) |
|
375 | } |
||
376 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: