Total Complexity | 75 |
Total Lines | 472 |
Duplicated Lines | 0 % |
Changes | 29 | ||
Bugs | 0 | Features | 0 |
Complex classes like DatalayerTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DatalayerTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | trait DatalayerTrait |
||
20 | { |
||
21 | /** @var PDO|null |
||
22 | * @deprecated |
||
23 | * */ |
||
24 | protected $instance = null; |
||
25 | |||
26 | /** @var string |
||
27 | * @deprecated |
||
28 | */ |
||
29 | protected string $fields; |
||
30 | |||
31 | /** @var PDOStatement|null |
||
32 | * @deprecated */ |
||
33 | protected $prepare = null; |
||
34 | |||
35 | /** @var string |
||
36 | * @deprecated |
||
37 | */ |
||
38 | protected $database = CONFIG_DATA_LAYER["dbname"]; |
||
39 | |||
40 | /** @var string |
||
41 | * @deprecated |
||
42 | */ |
||
43 | protected $classModel; |
||
44 | |||
45 | /** @var string |
||
46 | * @deprecated |
||
47 | */ |
||
48 | protected $tableName; |
||
49 | |||
50 | /** @var string */ |
||
51 | private $tableAlias; |
||
52 | |||
53 | /** @var array |
||
54 | */ |
||
55 | private array $data = []; |
||
56 | |||
57 | /** @var string */ |
||
58 | private $logSQL; |
||
59 | |||
60 | /** @var PDOException */ |
||
61 | private $error; |
||
62 | |||
63 | /** @var string */ |
||
64 | private string $query = ""; |
||
65 | |||
66 | /** @var array */ |
||
67 | private array $params = []; |
||
68 | |||
69 | |||
70 | /** @return PDO|false */ |
||
71 | private function getConnect() |
||
72 | { |
||
73 | try { |
||
74 | if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->getDatabase(), ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
||
75 | $database = $this->getDatabase().ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
||
76 | $this->setDatabase($database); |
||
77 | } |
||
78 | |||
79 | if (empty($this->instance)) { |
||
|
|||
80 | $this->instance = new PDO( |
||
81 | CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->getDatabase() . ';port=' . CONFIG_DATA_LAYER['port'], |
||
82 | CONFIG_DATA_LAYER['username'], |
||
83 | CONFIG_DATA_LAYER['passwd'], |
||
84 | CONFIG_DATA_LAYER['options'] |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | return $this->instance; |
||
89 | } catch (PDOException $e) { |
||
90 | $this->setError($e); |
||
91 | } |
||
92 | |||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param ?PDO $pdo |
||
97 | * @return Crud |
||
98 | * |
||
99 | */ |
||
100 | protected function setInstance(?PDO $pdo): self |
||
104 | } |
||
105 | |||
106 | protected function getInstance(): PDO |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $database |
||
113 | * @return $this |
||
114 | */ |
||
115 | protected function setDatabase(string $database): self |
||
116 | { |
||
117 | if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($database, ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
||
118 | $database = $database.ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
||
119 | $this->database = $database; |
||
120 | } else { |
||
121 | $this->database = $database; |
||
122 | } |
||
123 | |||
124 | if (!empty($this->instance)){ |
||
125 | $this->executeSQL("USE {$this->getDatabase()}"); |
||
126 | } |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | protected function getDatabase(): string |
||
135 | { |
||
136 | return $this->database ?? ""; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param string $fields |
||
141 | * @return Crud |
||
142 | */ |
||
143 | protected function setFields(string $fields): self |
||
144 | { |
||
145 | $this->fields = $fields; |
||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | protected function getFields():string |
||
153 | { |
||
154 | return $this->fields; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param string $tableName |
||
159 | * @param string $tableAlias |
||
160 | * @return CrudBuilder|Crud|DatalayerTrait |
||
161 | */ |
||
162 | protected function setTableName(string $tableName, string $tableAlias = ""): self |
||
163 | { |
||
164 | if (!empty($tableAlias)) |
||
165 | $this->tableAlias = $tableAlias; |
||
166 | $this->tableName = $tableName; |
||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | protected function getTableName(): string |
||
174 | { |
||
175 | return $this->tableName ?? ""; |
||
176 | } |
||
177 | |||
178 | protected function getTableAlias(): string |
||
179 | { |
||
180 | return $this->tableAlias ?? ""; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param string $classModel |
||
185 | * @return Crud |
||
186 | */ |
||
187 | protected function setClassModel(string $classModel): self |
||
188 | { |
||
189 | $this->classModel = $classModel; |
||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | protected function getClassModel(): string |
||
194 | { |
||
195 | return $this->classModel; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param string $classModel |
||
200 | * @return Crud |
||
201 | */ |
||
202 | protected function setPrepare(PDOStatement $prepare): self |
||
206 | } |
||
207 | |||
208 | protected function getPrepare(): PDOStatement |
||
209 | { |
||
210 | return $this->prepare; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param $params |
||
215 | * @return self |
||
216 | */ |
||
217 | protected function setParams($params): self |
||
218 | { |
||
219 | $this->params = array_merge($this->params, $params); |
||
220 | return $this; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param $params |
||
225 | * @return array |
||
226 | */ |
||
227 | protected function getParams(): array |
||
228 | { |
||
229 | return $this->params; |
||
230 | } |
||
231 | |||
232 | protected function getData(): array |
||
233 | { |
||
234 | return $this->data; |
||
235 | } |
||
236 | |||
237 | protected function setData(array $array): self |
||
238 | { |
||
239 | $this->data = $array; |
||
240 | return $this; |
||
241 | } |
||
242 | public function getQuery(): string |
||
243 | { |
||
244 | return $this->query; |
||
245 | } |
||
246 | public function setQuery(string $query): self |
||
247 | { |
||
248 | $this->query = $query; |
||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param string $query |
||
254 | * @param array|null $params |
||
255 | * @return PDOStatement|void |
||
256 | */ |
||
257 | protected function executeSQL(string $query, ?array $params = null) |
||
258 | { |
||
259 | try { |
||
260 | $this->setPrepare($this->getInstance()->prepare($query)); |
||
261 | $this->setSQL($query, $params); |
||
262 | $this->getPrepare()->execute($params); |
||
263 | return $this->getPrepare(); |
||
264 | } catch (PDOException $e) { |
||
265 | $this->setError($e); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param $prepare |
||
271 | * @return int|false |
||
272 | */ |
||
273 | protected function count(PDOStatement $prepare = null): ?int |
||
274 | { |
||
275 | try { |
||
276 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
277 | return $prepare->rowCount(); |
||
278 | } catch (PDOException $e) { |
||
279 | $this->setError($e);} |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param PDOStatement|null $prepare |
||
284 | * @return array|null |
||
285 | */ |
||
286 | protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array |
||
287 | { |
||
288 | try { |
||
289 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
290 | $dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
||
291 | $this->setData($dados); |
||
292 | return $dados; |
||
293 | } catch (PDOException $e) { |
||
294 | $this->setError($e); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param $prepare |
||
300 | * @return array|false |
||
301 | */ |
||
302 | protected function fetchArrayObj(PDOStatement $prepare = null): ?array |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param $prepare |
||
316 | * @param String|null $classModel |
||
317 | * @return array|false |
||
318 | */ |
||
319 | protected function fetchArrayClass(PDOStatement $prepare = null, string $classModel = null): ?array |
||
320 | { |
||
321 | try { |
||
322 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
323 | $classModel = empty($classModel) ? $this->getClassModel() : $classModel; |
||
324 | $dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $classModel); |
||
325 | $this->setData($dados); |
||
326 | return $dados; |
||
327 | } catch (PDOException $e) { |
||
328 | $this->setError($e); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param $prepare |
||
334 | * @return array|false |
||
335 | */ |
||
336 | protected function fetchOneAssoc(PDOStatement $prepare = null): ?array |
||
337 | { |
||
338 | try { |
||
339 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
340 | return $prepare->fetch(PDO::FETCH_ASSOC); |
||
341 | } catch (PDOException $e) { |
||
342 | $this->setError($e); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param PDOStatement|null $prepare |
||
348 | * @return stdClass|null |
||
349 | */ |
||
350 | protected function fetchOneObj(PDOStatement $prepare = null): ?stdClass |
||
351 | { |
||
352 | try { |
||
353 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
354 | return $prepare->fetch(PDO::FETCH_OBJ); |
||
355 | } catch (PDOException $e) { |
||
356 | $this->setError($e); |
||
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param $prepare |
||
362 | * @param String|null $class |
||
363 | * @return array|false |
||
364 | */ |
||
365 | protected function fetchOneClass(PDOStatement $prepare = null, string $class = null): ?object |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @return bool |
||
378 | */ |
||
379 | protected function beginTrasaction(): ?bool |
||
386 | } |
||
387 | |||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @return bool|null |
||
392 | */ |
||
393 | protected function commitTransaction(): ?bool |
||
394 | { |
||
395 | try { |
||
396 | $this->getInstance()->commit(); |
||
397 | return true; |
||
398 | } catch (PDOException $e) { |
||
399 | $this->setError($e); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @return bool|null |
||
405 | * |
||
406 | */ |
||
407 | protected function rollBackTransaction(): ?bool |
||
408 | { |
||
409 | |||
410 | try { |
||
411 | $this->getInstance()->rollBack(); |
||
412 | return true; |
||
413 | } catch (PDOException $e) { |
||
414 | $this->setError($e); |
||
415 | } |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @return string|null |
||
420 | * */ |
||
421 | private function lastId(): ?string |
||
422 | { |
||
423 | try { |
||
424 | return $this->getInstance()->lastInsertId(); |
||
425 | } catch (PDOException $e) { |
||
426 | $this->setError($e); |
||
427 | } |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * @param $sql_string |
||
432 | * @param array|null $params |
||
433 | * @return void |
||
434 | */ |
||
435 | private function setSQL($sql_string, ?array $params = null) |
||
436 | { |
||
437 | try { |
||
438 | if (!empty($params)) { |
||
439 | $indexed = $params == array_values($params); |
||
440 | foreach ($params as $k => $v) { |
||
441 | if (is_object($v)) { |
||
442 | if ($v instanceof \DateTime) { |
||
443 | $v = $v->format('Y-m-d H:i:s'); |
||
444 | } else { |
||
445 | continue; |
||
446 | } |
||
447 | } elseif (is_string($v)) { |
||
448 | $v = "'$v'"; |
||
449 | } elseif ($v === null) { |
||
450 | $v = 'NULL'; |
||
451 | } elseif (is_array($v)) { |
||
452 | $v = implode(',', $v); |
||
453 | } |
||
454 | |||
455 | if ($indexed) { |
||
456 | $sql_string = preg_replace('/\?/', $v, $sql_string, 1); |
||
457 | } else { |
||
458 | if ($k[0] != ':') { |
||
459 | $k = ':' . $k; |
||
460 | } //add leading colon if it was left out |
||
461 | $sql_string = str_replace($k, $v, $sql_string); |
||
462 | } |
||
463 | } |
||
464 | } |
||
465 | $this->logSQL = $sql_string; |
||
466 | } catch (PDOException $e) { |
||
467 | $this->setError($e); |
||
468 | } |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * @return string|null |
||
473 | */ |
||
474 | protected function getSQL(): ?string |
||
480 | } |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @param PDOException $e |
||
485 | * @return void |
||
486 | */ |
||
487 | protected function setError(PDOException $e) |
||
488 | { |
||
489 | $this->error = $e; |
||
490 | throw new PDOException("{$e->getMessage()}<br/><b>SQL:</b> {$this->getSQL()}"); |
||
491 | |||
492 | } |
||
493 | } |
||
494 |