Total Complexity | 67 |
Total Lines | 423 |
Duplicated Lines | 0 % |
Changes | 24 | ||
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 $resultArray = []; |
||
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 |
||
107 | { |
||
108 | return $this->instance ?? $this->getConnect(); |
||
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($this->getDatabase(), ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
||
118 | $database = $database.ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
||
119 | } |
||
120 | |||
121 | $this->database = $database; |
||
122 | $this->setInstance(null)->getInstance(); |
||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function getDatabase(): string |
||
130 | { |
||
131 | return $this->database ?? ""; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $fields |
||
136 | * @return Crud |
||
137 | */ |
||
138 | protected function setFields(string $fields): self |
||
139 | { |
||
140 | $this->fields = $fields; |
||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function getFields():string |
||
148 | { |
||
149 | return $this->fields; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param string $tableName |
||
154 | * @param string $tableAlias |
||
155 | * @return CrudBuilder|Crud|DatalayerTrait |
||
156 | */ |
||
157 | protected function setTable(string $tableName, string $tableAlias = ""): self |
||
158 | { |
||
159 | if (!empty($tableAlias)) |
||
160 | $this->tableAlias = $tableAlias; |
||
161 | $this->tableName = $tableName; |
||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | protected function getTable(): string |
||
169 | { |
||
170 | return $this->tableName ?? ""; |
||
171 | } |
||
172 | |||
173 | protected function getTableAlias(): string |
||
174 | { |
||
175 | return $this->tableAlias ?? ""; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $classModel |
||
180 | * @return Crud |
||
181 | */ |
||
182 | protected function setClassModel(string $classModel): self |
||
183 | { |
||
184 | $this->classModel = $classModel; |
||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | protected function getClassModel(): string |
||
189 | { |
||
190 | return $this->classModel; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $classModel |
||
195 | * @return Crud |
||
196 | */ |
||
197 | protected function setPrepare(PDOStatement $prepare): self |
||
198 | { |
||
199 | $this->prepare = $prepare; |
||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | protected function getPrepare(): PDOStatement |
||
204 | { |
||
205 | return $this->prepare; |
||
206 | } |
||
207 | |||
208 | protected function getResult(): array |
||
209 | { |
||
210 | return $this->resultArray; |
||
211 | } |
||
212 | |||
213 | protected function setResult(array $array): self |
||
214 | { |
||
215 | $this->resultArray = $array; |
||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param string $query |
||
221 | * @param array|null $params |
||
222 | * @return PDOStatement|void |
||
223 | */ |
||
224 | protected function executeSQL(string $query, ?array $params = null) |
||
225 | { |
||
226 | try { |
||
227 | $this->setPrepare($this->getInstance()->prepare($query)); |
||
228 | $this->setLogSQL($query, $params); |
||
229 | $this->getPrepare()->execute($params); |
||
230 | return $this->getPrepare(); |
||
231 | } catch (PDOException $e) { |
||
232 | $this->setError($e); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param $prepare |
||
238 | * @return int|false |
||
239 | */ |
||
240 | protected function count(PDOStatement $prepare = null): ?int |
||
241 | { |
||
242 | try { |
||
243 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
244 | return $prepare->rowCount(); |
||
245 | } catch (PDOException $e) { |
||
246 | $this->setError($e);} |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param PDOStatement|null $prepare |
||
251 | * @return array|null |
||
252 | */ |
||
253 | protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array |
||
254 | { |
||
255 | try { |
||
256 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
257 | $dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
||
258 | $this->setResult($dados); |
||
259 | return $dados; |
||
260 | } catch (PDOException $e) { |
||
261 | $this->setError($e); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @param $prepare |
||
267 | * @return array|false |
||
268 | */ |
||
269 | protected function fetchArrayObj(PDOStatement $prepare = null): ?array |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param $prepare |
||
283 | * @param String|null $classModel |
||
284 | * @return array|false |
||
285 | */ |
||
286 | protected function fetchArrayClass(PDOStatement $prepare = null, string $classModel = null): ?array |
||
287 | { |
||
288 | try { |
||
289 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
290 | $classModel = empty($classModel) ? $this->getClassModel() : $classModel; |
||
291 | $dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $classModel); |
||
292 | $this->setResult($dados); |
||
293 | return $dados; |
||
294 | } catch (PDOException $e) { |
||
295 | $this->setError($e); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param $prepare |
||
301 | * @return array|false |
||
302 | */ |
||
303 | protected function fetchOneAssoc(PDOStatement $prepare = null): ?array |
||
304 | { |
||
305 | try { |
||
306 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
307 | return $prepare->fetch(PDO::FETCH_ASSOC); |
||
308 | } catch (PDOException $e) { |
||
309 | $this->setError($e); |
||
310 | } |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param PDOStatement|null $prepare |
||
315 | * @return stdClass|null |
||
316 | */ |
||
317 | protected function fetchOneObj(PDOStatement $prepare = null): ?stdClass |
||
318 | { |
||
319 | try { |
||
320 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
321 | return $prepare->fetch(PDO::FETCH_OBJ); |
||
322 | } catch (PDOException $e) { |
||
323 | $this->setError($e); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @param $prepare |
||
329 | * @param String|null $class |
||
330 | * @return array|false |
||
331 | */ |
||
332 | protected function fetchOneClass(PDOStatement $prepare = null, string $class = null): ?object |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @return bool |
||
345 | */ |
||
346 | protected function beginTrasaction(): ?bool |
||
353 | } |
||
354 | |||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @return bool|null |
||
359 | */ |
||
360 | protected function commitTransaction(): ?bool |
||
361 | { |
||
362 | try { |
||
363 | $this->getInstance()->commit(); |
||
364 | return true; |
||
365 | } catch (PDOException $e) { |
||
366 | $this->setError($e); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @return bool|null |
||
372 | * |
||
373 | */ |
||
374 | protected function rollBackTransaction(): ?bool |
||
382 | } |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return string|null |
||
387 | * */ |
||
388 | private function lastId(): ?string |
||
389 | { |
||
390 | try { |
||
391 | return $this->getInstance()->lastInsertId(); |
||
392 | } catch (PDOException $e) { |
||
393 | $this->setError($e); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @param $sql_string |
||
399 | * @param array|null $params |
||
400 | * @return void |
||
401 | */ |
||
402 | private function setLogSQL($sql_string, ?array $params = null) |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param PDOException $e |
||
436 | * @return void |
||
437 | */ |
||
438 | private function setError(PDOException $e) |
||
439 | { |
||
440 | $this->error = $e; |
||
441 | throw new PDOException("{$e->getMessage()}<br/><b>SQL:</b> {$this->getSQL()}"); |
||
442 | |||
443 | } |
||
444 | } |
||
445 |