Passed
Push — master ( 0272f5...3a0c5f )
by Aleksandr
28:04 queued 25:53
created

Model   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
dl 0
loc 114
ccs 56
cts 58
cp 0.9655
rs 10
c 1
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A db() 0 3 1
A getTable() 0 3 1
A setPrimaryKey() 0 3 1
A setTable() 0 3 1
A getPrimaryKey() 0 3 1
A deleteByKey() 0 11 1
A updateByArray() 0 23 2
A count() 0 8 1
A findByKey() 0 10 1
A insert() 0 18 2
A getNativeConnection() 0 3 1
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 string $primaryKey = 'id';
19
20
    protected string $table;
21
22 1
    public function db() : DBALConnection
23
    {
24 1
        return Connection::get();
25
    }
26
27
    public function getNativeConnection() : PDO
28
    {
29
        return Connection::getNativeConnection();
30
    }
31
32 1
    public function setPrimaryKey(string $name): void
33
    {
34 1
        $this->primaryKey = $name;
35
    }
36
37 1
    public function getPrimaryKey(): string
38
    {
39 1
        return $this->primaryKey;
40
    }
41
42 1
    public function setTable(string $table): void
43
    {
44 1
        $this->table = $table;
45
    }
46
47 1
    public function getTable() : string
48
    {
49 1
        return $this->table;
50
    }
51
52 1
    public function insert(array $data): int
53
    {
54 1
        $conn = $this->db()->getNativeConnection();
55 1
        $table = $this->getTable();
56
57
        // Extract the column names and values from the data array
58 1
        $columns = implode(', ', array_keys($data));
59 1
        $placeholders = ':' . implode(', :', array_keys($data));
60
61
        // Prepare the SQL statement
62 1
        $stmt = $conn->prepare("INSERT INTO $table ($columns) VALUES ($placeholders)");
63
        // Bind the values from the $data array to the prepared statement
64 1
        foreach ($data as $key => $value) {
65 1
            $stmt->bindValue(':' . $key, $value);
66
        }
67 1
        $stmt->execute();
68
69 1
        return (int) $conn->lastInsertId();
70
    }
71
72 1
    public function updateByArray(int $key, array $data) : bool
73
    {
74 1
        $keyName = $this->getPrimaryKey();
75
76 1
        $conn = $this->db()->getNativeConnection();
77 1
        $table = $this->getTable();
78
79
        // Prepare the update statement
80 1
        $columns = array_keys($data);
81 1
        $setValues = implode(' = ?, ', $columns) . ' = ?';
82
83 1
        $stmt = $conn->prepare("UPDATE $table SET $setValues WHERE $keyName = :key");
84
85
        // Bind the data parameters
86 1
        $index = 1;
87 1
        foreach ($data as $value) {
88 1
            $stmt->bindValue($index, $value);
89 1
            $index++;
90
        }
91
92
        // Bind the ID parameter
93 1
        $stmt->bindValue(':key',  $key, PDO::PARAM_INT);
94 1
        return $stmt->execute();
95
    }
96
97 1
    public function deleteByKey(int $key): bool
98
    {
99 1
        $keyName = $this->getPrimaryKey();
100
101 1
        $conn = $this->db()->getNativeConnection();
102 1
        $table = $this->getTable();
103
104 1
        $stmt = $conn->prepare("DELETE FROM $table WHERE $keyName = :key");
105 1
        $stmt->bindValue(':key',  $key, PDO::PARAM_INT);
106
107 1
        return $stmt->execute();
108
    }
109
110 1
    public function count() : int
111
    {
112 1
        $conn = $this->db()->getNativeConnection();
113 1
        $table = $this->getTable();
114 1
        $stmt = $conn->prepare("SELECT COUNT(*) as count FROM $table");
115 1
        $stmt->execute();
116 1
        $record = $stmt->fetch(PDO::FETCH_ASSOC);
117 1
        return (int) $record["count"];
118
    }
119
120 1
    public function findByKey(int $key) : array|false
121
    {
122 1
        return $this->db()
123 1
            ->createQueryBuilder()
124 1
            ->select('*')
125 1
            ->from($this->getTable())
126 1
            ->where(sprintf('%s = ?', $this->getPrimaryKey()))
127 1
            ->setParameter(0, $key, PDO::PARAM_INT)
128 1
            ->executeQuery()
129 1
            ->fetchAssociative();
130
    }
131
}