Passed
Push — master ( da1b99...92df87 )
by Aleksandr
03:12
created

Model::connection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Aleksandr Drobotik <[email protected]>
5
 * @copyright 2023 Aleksandr Drobotik
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
declare(strict_types=1);
9
10
namespace Drobotik\Eav\Model;
11
12
use Drobotik\Eav\Database\Connection;
13
use PDO;
14
15
class Model
16
{
17
    protected int $key;
18
    protected string $keyName = 'id';
19
20
    protected string $table;
21
22 3
    public function connection() : PDO
23
    {
24 3
        return Connection::pdo();
25
    }
26
27 1
    public function isKey() : bool
28
    {
29 1
        return isset($this->key) && $this->key > 0;
30
    }
31
32 3
    public function setKey($key): void
33
    {
34 3
        $this->key = $key;
35
    }
36
37 3
    public function getKey(): int
38
    {
39 3
        return $this->key;
40
    }
41
42 2
    public function setKeyName(string $name): void
43
    {
44 2
        $this->keyName = $name;
45
    }
46
47 2
    public function getKeyName(): string
48
    {
49 2
        return $this->keyName;
50
    }
51
52 3
    public function setTable(string $table): void
53
    {
54 3
        $this->table = $table;
55
    }
56
57 3
    public function getTable() : string
58
    {
59 3
        return $this->table;
60
    }
61
62 1
    public function insert(array $data): int
63
    {
64 1
        $conn = $this->connection();
65 1
        $table = $this->getTable();
66
67
        // Extract the column names and values from the data array
68 1
        $columns = implode(', ', array_keys($data));
69 1
        $placeholders = ':' . implode(', :', array_keys($data));
70
71
        // Prepare the SQL statement
72 1
        $stmt = $conn->prepare("INSERT INTO $table ($columns) VALUES ($placeholders)");
73
        // Bind the values from the $data array to the prepared statement
74 1
        foreach ($data as $key => $value) {
75 1
            $stmt->bindValue(':' . $key, $value);
76
        }
77 1
        $stmt->execute();
78
79 1
        $key = (int) $conn->lastInsertId();
80 1
        $this->setKey($key);
81
82 1
        return $key;
83
    }
84
85 1
    public function update(array $data) : bool
86
    {
87 1
        $key =  $this->getKey();
88 1
        $keyName = $this->getKeyName();
89
90 1
        $conn = $this->connection();
91 1
        $table = $this->getTable();
92
93
        // Prepare the update statement
94 1
        $columns = array_keys($data);
95 1
        $setValues = implode(' = ?, ', $columns) . ' = ?';
96
97 1
        $stmt = $conn->prepare("UPDATE $table SET $setValues WHERE $keyName = :key");
98
99
        // Bind the data parameters
100 1
        $index = 1;
101 1
        foreach ($data as $value) {
102 1
            $stmt->bindValue($index, $value);
103 1
            $index++;
104
        }
105
106
        // Bind the ID parameter
107 1
        $stmt->bindValue(':key',  $key, PDO::PARAM_INT);
108 1
        return $stmt->execute();
109
    }
110
111 1
    public function delete(): bool
112
    {
113 1
        $key =  $this->getKey();
114 1
        $keyName = $this->getKeyName();
115
116 1
        $conn = $this->connection();
117 1
        $table = $this->getTable();
118
119 1
        $stmt = $conn->prepare("DELETE FROM $table WHERE $keyName = :key");
120 1
        $stmt->bindValue(':key',  $key, PDO::PARAM_INT);
121
122 1
        return $stmt->execute();
123
    }
124
125 1
    public function toArray() : array
126
    {
127 1
        $result = [];
128 1
        if($this->isKey())
129
        {
130 1
            $result[$this->getKeyName()] = $this->getKey();
131
        }
132 1
        return $result;
133
    }
134
135 1
    public function count() : int
136
    {
137 1
        $conn = $this->connection();
138 1
        $table = $this->getTable();
139 1
        $stmt = $conn->prepare("SELECT COUNT(*) as count FROM $table");
140 1
        $stmt->execute();
141 1
        $record = $stmt->fetch(PDO::FETCH_ASSOC);
142 1
        return (int) $record["count"];
143
    }
144
145 1
    public function findMe()
146
    {
147 1
        $conn = $this->connection();
148 1
        $table = $this->getTable();
149 1
        $key = $this->getKey();
150 1
        $keyName = $this->getKeyName();
151 1
        $stmt = $conn->prepare("SELECT * FROM $table WHERE $keyName = :key");
152 1
        $stmt->bindValue(':key', $key, PDO::PARAM_INT);
153 1
        $stmt->execute();
154 1
        return $stmt->fetch(PDO::FETCH_ASSOC);
155
    }
156
}