Total Complexity | 62 |
Total Lines | 407 |
Duplicated Lines | 0 % |
Changes | 20 | ||
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 | * @deprecated |
||
55 | */ |
||
56 | protected $resultArray = array(); |
||
57 | |||
58 | /** @var string */ |
||
59 | private $logSQL; |
||
60 | |||
61 | /** @var PDOException */ |
||
62 | private $error; |
||
63 | |||
64 | /** @var string */ |
||
65 | private string $query = ""; |
||
66 | |||
67 | /** @var array */ |
||
68 | private array $params = []; |
||
69 | |||
70 | |||
71 | /** @return PDO|false */ |
||
72 | private function getInstance() |
||
73 | { |
||
74 | try { |
||
75 | if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->getDatabase(), ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
||
76 | $database = $this->getDatabase().ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
||
77 | $this->setDatabase($database); |
||
78 | } |
||
79 | |||
80 | if (empty($this->getInstance())) { |
||
81 | $instance = new PDO( |
||
82 | CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->getDatabase() . ';port=' . CONFIG_DATA_LAYER['port'], |
||
83 | CONFIG_DATA_LAYER['username'], |
||
84 | CONFIG_DATA_LAYER['passwd'], |
||
85 | CONFIG_DATA_LAYER['options'] |
||
86 | ); |
||
87 | $this->setInstance($instance); |
||
88 | } |
||
89 | |||
90 | return $this->getInstance(); |
||
91 | } catch (PDOException $e) { |
||
92 | $this->setError($e); |
||
93 | } |
||
94 | |||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param ?PDO $pdo |
||
99 | * @return Crud |
||
100 | * |
||
101 | */ |
||
102 | protected function setInstance(?PDO $pdo): self |
||
103 | { |
||
104 | $this->instance = $pdo; |
||
|
|||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param string $database |
||
110 | * @return $this |
||
111 | */ |
||
112 | protected function setDatabase(string $database): self |
||
113 | { |
||
114 | $this->database = $database; |
||
115 | $this->setInstance(null); |
||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function getDatabase(): string |
||
123 | { |
||
124 | return $this->database ?? ""; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param string $fields |
||
129 | * @return Crud |
||
130 | */ |
||
131 | protected function setFields(string $fields): self |
||
132 | { |
||
133 | $this->fields = $fields; |
||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | protected function getFields():string |
||
141 | { |
||
142 | return $this->fields; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param string $tableName |
||
147 | * @param string $tableAlias |
||
148 | * @return CrudBuilder|Crud|DatalayerTrait |
||
149 | */ |
||
150 | protected function setTable(string $tableName, string $tableAlias = ""): self |
||
151 | { |
||
152 | if (!empty($tableAlias)) |
||
153 | $this->tableAlias = $tableAlias; |
||
154 | $this->tableName = $tableName; |
||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | protected function getTable(): string |
||
162 | { |
||
163 | return $this->tableName ?? ""; |
||
164 | } |
||
165 | |||
166 | protected function getTableAlias(): string |
||
167 | { |
||
168 | return $this->tableAlias ?? ""; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param string $classModel |
||
173 | * @return Crud |
||
174 | */ |
||
175 | protected function setClassModel(string $classModel): self |
||
176 | { |
||
177 | $this->classModel = $classModel; |
||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | protected function getClassModel(): string |
||
182 | { |
||
183 | return $this->classModel; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param string $classModel |
||
188 | * @return Crud |
||
189 | */ |
||
190 | protected function setPrepare(PDOStatement $prepare): self |
||
191 | { |
||
192 | $this->prepare = $prepare; |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | protected function getPrepare(): PDOStatement |
||
197 | { |
||
198 | return $this->prepare; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param string $query |
||
203 | * @param array|null $params |
||
204 | * @return PDOStatement|void |
||
205 | */ |
||
206 | protected function executeSQL(string $query, ?array $params = null) |
||
207 | { |
||
208 | try { |
||
209 | $this->setPrepare($this->getInstance()->prepare($query)); |
||
210 | $this->setLogSQL($query, $params); |
||
211 | $this->getPrepare()->execute($params); |
||
212 | return $this->getPrepare(); |
||
213 | } catch (PDOException $e) { |
||
214 | $this->setError($e); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param $prepare |
||
220 | * @return int|false |
||
221 | */ |
||
222 | protected function count(PDOStatement $prepare = null): ?int |
||
223 | { |
||
224 | try { |
||
225 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
226 | return $prepare->rowCount(); |
||
227 | } catch (PDOException $e) { |
||
228 | $this->setError($e);} |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param $prepare |
||
233 | * @return array|false |
||
234 | */ |
||
235 | protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array |
||
236 | { |
||
237 | try { |
||
238 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
239 | $dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
||
240 | $this->resultArray = $dados; |
||
241 | return $dados; |
||
242 | } catch (PDOException $e) { |
||
243 | $this->setError($e); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @param $prepare |
||
249 | * @return array|false |
||
250 | */ |
||
251 | protected function fetchArrayObj(PDOStatement $prepare = null): ?array |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @param $prepare |
||
265 | * @param String|null $class |
||
266 | * @return array|false |
||
267 | */ |
||
268 | protected function fetchArrayClass(PDOStatement $prepare = null, string $class = null): ?array |
||
269 | { |
||
270 | try { |
||
271 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
272 | $class = empty($class) ? $this->classModel : $class; |
||
273 | $dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $class); |
||
274 | $this->resultArray = $dados; |
||
275 | return $dados; |
||
276 | } catch (PDOException $e) { |
||
277 | $this->setError($e); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param $prepare |
||
283 | * @return array|false |
||
284 | */ |
||
285 | protected function fetchOneAssoc(PDOStatement $prepare = null): ?array |
||
286 | { |
||
287 | try { |
||
288 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
289 | $dados = $prepare->fetch(PDO::FETCH_ASSOC); |
||
290 | return $dados; |
||
291 | } catch (PDOException $e) { |
||
292 | $this->setError($e); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @param $prepare |
||
298 | * @return stdClass|false |
||
299 | */ |
||
300 | protected function fetchOneObj(PDOStatement $prepare = null): ?stdClass |
||
301 | { |
||
302 | try { |
||
303 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
304 | $dados = $prepare->fetch(PDO::FETCH_OBJ); |
||
305 | return $dados; |
||
306 | } catch (PDOException $e) { |
||
307 | $this->setError($e); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param $prepare |
||
313 | * @param String|null $class |
||
314 | * @return array|false |
||
315 | */ |
||
316 | protected function fetchOneClass(PDOStatement $prepare = null, string $class = null): ?object |
||
324 | } |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @return bool |
||
329 | */ |
||
330 | protected function beginTrasaction(): ?bool |
||
337 | } |
||
338 | |||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @return bool|null |
||
343 | */ |
||
344 | protected function commitTransaction(): ?bool |
||
345 | { |
||
346 | try { |
||
347 | $this->getInstance()->commit(); |
||
348 | return true; |
||
349 | } catch (PDOException $e) { |
||
350 | $this->setError($e); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @return bool|null |
||
356 | * |
||
357 | */ |
||
358 | protected function rollBackTransaction(): ?bool |
||
366 | } |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @return string|null |
||
371 | * */ |
||
372 | private function lastId(): ?string |
||
373 | { |
||
374 | try { |
||
375 | return $this->getInstance()->lastInsertId(); |
||
376 | } catch (PDOException $e) { |
||
377 | $this->setError($e); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @param $sql_string |
||
383 | * @param array|null $params |
||
384 | * @return void |
||
385 | */ |
||
386 | private function setLogSQL($sql_string, ?array $params = null) |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @param PDOException $e |
||
420 | * @return void |
||
421 | */ |
||
422 | private function setError(PDOException $e) |
||
423 | { |
||
424 | $this->error = $e; |
||
425 | throw new PDOException("{$e->getMessage()}<br/><b>SQL:</b> {$this->getSQL()}"); |
||
426 | |||
427 | } |
||
429 |