DataLayerTrait::create()   A
last analyzed

Complexity

Conditions 3
Paths 12

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 18
rs 9.8666
1
<?php
2
3
namespace CodefusionTM\DataLayer;
4
5
use DateTime;
6
use Exception;
7
use PDOException;
8
9
/**
10
 * Trait DataLayerTrait
11
 * @package CodefusionTM\DataLayer
12
 */
13
trait DataLayerTrait
14
{
15
    /**
16
     * @param array $data
17
     * @return int|null
18
     * @throws Exception
19
     */
20
    protected function create(array $data): ?int
21
    {
22
        if ($this->timestamps) {
23
            $data["created_at"] = (new DateTime("now"))->format("Y-m-d H:i:s");
24
            $data["updated_at"] = $data["created_at"];
25
        }
26
27
        try {
28
            $columns = implode(", ", array_keys($data));
29
            $values = ":" . implode(", :", array_keys($data));
30
31
            $stmt = Connect::getInstance()->prepare("INSERT INTO {$this->entity} ({$columns}) VALUES ({$values})");
32
            $stmt->execute($this->filter($data));
33
34
            return Connect::getInstance()->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return CodefusionTM\Data...tance()->lastInsertId() returns the type string which is incompatible with the type-hinted return integer|null.
Loading history...
35
        } catch (PDOException $exception) {
36
            $this->fail = $exception;
0 ignored issues
show
Bug Best Practice introduced by
The property fail does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
            return null;
38
        }
39
    }
40
41
    /**
42
     * @param array $data
43
     * @param string $terms
44
     * @param string $params
45
     * @return int|null
46
     * @throws Exception
47
     */
48
    protected function update(array $data, string $terms, string $params): ?int
49
    {
50
        if ($this->timestamps) {
51
            $data["updated_at"] = (new DateTime("now"))->format("Y-m-d H:i:s");
52
        }
53
54
        try {
55
            $dateSet = [];
56
            foreach ($data as $bind => $value) {
57
                $dateSet[] = "{$bind} = :{$bind}";
58
            }
59
            $dateSet = implode(", ", $dateSet);
60
            parse_str($params, $params);
0 ignored issues
show
Bug introduced by
$params of type string is incompatible with the type array expected by parameter $result 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

60
            parse_str($params, /** @scrutinizer ignore-type */ $params);
Loading history...
61
62
            $stmt = Connect::getInstance()->prepare("UPDATE {$this->entity} SET {$dateSet} WHERE {$terms}");
63
            $stmt->execute($this->filter(array_merge($data, $params)));
64
            return ($stmt->rowCount() ?? 1);
65
        } catch (PDOException $exception) {
66
            $this->fail = $exception;
0 ignored issues
show
Bug Best Practice introduced by
The property fail does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
            return null;
68
        }
69
    }
70
71
    /**
72
     * @param string $terms
73
     * @param string|null $params
74
     * @return bool
75
     */
76
    public function delete(string $terms, ?string $params): bool
77
    {
78
        try {
79
            $stmt = Connect::getInstance()->prepare("DELETE FROM {$this->entity} WHERE {$terms}");
80
            if ($params) {
81
                parse_str($params, $params);
0 ignored issues
show
Bug introduced by
$params of type string is incompatible with the type array expected by parameter $result 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

81
                parse_str($params, /** @scrutinizer ignore-type */ $params);
Loading history...
82
                $stmt->execute($params);
83
                return true;
84
            }
85
86
            $stmt->execute();
87
            return true;
88
        } catch (PDOException $exception) {
89
            $this->fail = $exception;
0 ignored issues
show
Bug Best Practice introduced by
The property fail does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
90
            return false;
91
        }
92
    }
93
94
    /**
95
     * @param array $data
96
     * @return array|null
97
     */
98
    private function filter(array $data): ?array
99
    {
100
        $filter = [];
101
        foreach ($data as $key => $value) {
102
            $filter[$key] = (is_null($value) ? null : filter_var($value, FILTER_DEFAULT));
103
        }
104
        return $filter;
105
    }
106
}