Passed
Push — master ( feac44...0272f5 )
by Aleksandr
33:21
created

Model::getNativeConnection()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 1
    public function getNativeConnection() : PDO
28
    {
29 1
        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 1
    }
51
52
    public function insert(array $data): int
53 1
    {
54 1
        $conn = $this->db()->getNativeConnection();
55
        $table = $this->getTable();
56
57 1
        // Extract the column names and values from the data array
58
        $columns = implode(', ', array_keys($data));
59 1
        $placeholders = ':' . implode(', :', array_keys($data));
60 1
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
            $stmt->bindValue(':' . $key, $value);
66
        }
67 1
        $stmt->execute();
68
69 1
        return (int) $conn->lastInsertId();
70
    }
71 1
72 1
    public function updateByArray(int $key, array $data) : bool
73
    {
74
        $keyName = $this->getPrimaryKey();
75 1
76 1
        $conn = $this->db()->getNativeConnection();
77
        $table = $this->getTable();
78 1
79
        // Prepare the update statement
80
        $columns = array_keys($data);
81 1
        $setValues = implode(' = ?, ', $columns) . ' = ?';
82 1
83 1
        $stmt = $conn->prepare("UPDATE $table SET $setValues WHERE $keyName = :key");
84 1
85
        // Bind the data parameters
86
        $index = 1;
87
        foreach ($data as $value) {
88 1
            $stmt->bindValue($index, $value);
89 1
            $index++;
90
        }
91
92 1
        // Bind the ID parameter
93
        $stmt->bindValue(':key',  $key, PDO::PARAM_INT);
94 1
        return $stmt->execute();
95
    }
96 1
97 1
    public function deleteByKey(int $key): bool
98
    {
99 1
        $keyName = $this->getPrimaryKey();
100 1
101
        $conn = $this->db()->getNativeConnection();
102 1
        $table = $this->getTable();
103
104
        $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 1
    }
109 1
110 1
    public function count() : int
111 1
    {
112 1
        $conn = $this->db()->getNativeConnection();
113
        $table = $this->getTable();
114
        $stmt = $conn->prepare("SELECT COUNT(*) as count FROM $table");
115 1
        $stmt->execute();
116
        $record = $stmt->fetch(PDO::FETCH_ASSOC);
117 1
        return (int) $record["count"];
118 1
    }
119 1
120 1
    public function findByKey(int $key) : array|false
121 1
    {
122 1
        return $this->db()
123 1
            ->createQueryBuilder()
124 1
            ->select('*')
125
            ->from($this->getTable())
126
            ->where(sprintf('%s = ?', $this->getPrimaryKey()))
127
            ->setParameter(0, $key, PDO::PARAM_INT)
128
            ->executeQuery()
129
            ->fetchAssociative();
130
    }
131
}