Passed
Push — master ( 83edb5...5bb122 )
by Aleksandr
02:51
created

Model   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 109
ccs 56
cts 56
cp 1
rs 10
wmc 12

10 Methods

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