Passed
Push — master ( 023570...3c9f2b )
by Aleksandr
02:49
created

Model::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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