Passed
Branch master (fc6eea)
by Henri
02:26 queued 01:04
created

CrudTrait::check_fail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
use Exception;
6
use PDOException;
7
use PDO;
8
9
trait CrudTrait{
10
11
    protected ?Exception $fail = null;
12
13
    protected function check_fail()
14
    {
15
        if(!is_null($this->fail)){
16
            throw $this->fail;
17
        }
18
    }
19
20
    protected function transaction(string $transaction): ?bool
21
    {
22
        switch ($transaction) {
23
            case 'begin': return (Connect::getInstance()->inTransaction()) ? Connect::getInstance()->beginTransaction() : false;
24
            case 'commit': return (Connect::getInstance()->inTransaction()) ? Connect::getInstance()->commit() : false;
25
            case 'rollback': return (Connect::getInstance()->inTransaction()) ? Connect::getInstance()->rollBack() : false;
26
        }
27
        return false;
28
    }
29
30
    protected function select(string $query,array $data): ?array
31
    {
32
        try{
33
            $stmt = Connect::getInstance()->prepare("{$query}");
34
            $stmt->execute($data);
35
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
36
        }catch(Exception $exception){
37
            $this->fail = $exception;
38
        }
39
        return [];
40
    }
41
42
    protected function describe(): ?array
43
    {
44
        try{
45
            $stmt = Connect::getInstance()->prepare("DESCRIBE {$this->table}");
46
            $stmt->execute();
47
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
48
        }catch(Exception $exception){
49
            $this->fail = $exception;
50
            return null;
51
        }
52
    }
53
54
    protected function insert(array $data): ?int
55
    {
56
        try {
57
            $columns = implode(", ", array_keys($data));
58
            $values = ":" . implode(", :", array_keys($data));
59
60
            $stmt = Connect::getInstance()->prepare("INSERT INTO {$this->table} ({$columns}) VALUES ({$values})");
61
62
            $stmt->execute($this->filter($data));
63
64
            return Connect::getInstance()->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return HnrAzevedo\Datama...tance()->lastInsertId() returns the type string which is incompatible with the type-hinted return integer|null.
Loading history...
65
        } catch (PDOException $exception) {
66
            $this->fail = $exception;
67
            return null;
68
        }
69
    }
70
71
    protected function update(array $data, string $terms, string $params): ?int
72
    {
73
        try {
74
            $dateSet = [];
75
            foreach ($data as $bind => $value) {
76
                $dateSet[] = "{$bind} = :{$bind}";
77
            }
78
            $dateSet = implode(", ", $dateSet);
79
            parse_str($params, $params);
0 ignored issues
show
Bug introduced by
$params of type string is incompatible with the type array|null expected by parameter $arr of parse_str(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            parse_str($params, /** @scrutinizer ignore-type */ $params);
Loading history...
80
81
            $stmt = Connect::getInstance()->prepare("UPDATE {$this->table} SET {$dateSet} WHERE {$terms}");
82
            $stmt->execute($this->filter(array_merge($data, $params)));
83
            return ($stmt->rowCount() ?? 1);
84
        } catch (PDOException $exception) {
85
            $this->fail = $exception;
86
            return null;
87
        }
88
    }
89
90
    public function delete(string $terms, ?string $params): bool
91
    {
92
        try {
93
            $stmt = Connect::getInstance()->prepare("DELETE FROM {$this->table} WHERE {$terms}");
94
95
            if($params){
96
                parse_str($params, $params);
0 ignored issues
show
Bug introduced by
$params of type string is incompatible with the type array|null expected by parameter $arr of parse_str(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
                parse_str($params, /** @scrutinizer ignore-type */ $params);
Loading history...
97
                $stmt->execute($params);
98
                return true;
99
            }
100
101
            $stmt->execute();
102
            return true;
103
        } catch (PDOException $exception) {
104
            $this->fail = $exception;
105
            return false;
106
        }
107
    }
108
109
    private function filter(array $data): ?array
110
    {
111
        $filter = [];
112
        foreach ($data as $key => $value) {
113
            $filter[$key] = (is_null($value) ? null : filter_var($value, FILTER_DEFAULT));
114
        }
115
        return $filter;
116
    }
117
118
}
119