Passed
Push — master ( 022b32...35c6c5 )
by Aleksandr
01:52
created

Model::findByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
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;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use PDO;
15
16
class Model
17
{
18
    protected string $primaryKey = 'id';
19
20
    protected string $table;
21
22 1
    public function db() : PDO
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();
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();
77 1
        $table = $this->getTable();
78
79 1
        $setValues = [];
80 1
        foreach (array_keys($data) as $column) {
81 1
            $setValues[] = "$column = :$column";
82
        }
83 1
        $setValues = implode(", ", $setValues);
84
85 1
        $stmt = $conn->prepare("UPDATE $table SET $setValues WHERE $keyName = :key");
86
87 1
        foreach ($data as $k => $value) {
88 1
            $stmt->bindValue($k, $value);
89
        }
90
91 1
        $binds = array_merge($data, ['key' => $key]);
92 1
        return $stmt->execute($binds);
93
    }
94
95 1
    public function deleteByKey(int $key): bool
96
    {
97 1
        $keyName = $this->getPrimaryKey();
98
99 1
        $conn = $this->db();
100 1
        $table = $this->getTable();
101
102 1
        $stmt = $conn->prepare("DELETE FROM $table WHERE $keyName = :key");
103 1
        $stmt->bindValue(':key',  $key, PDO::PARAM_INT);
104
105 1
        return $stmt->execute();
106
    }
107
108 1
    public function count() : int
109
    {
110 1
        $table = $this->getTable();
111 1
        $stmt = $this->db()->prepare("SELECT COUNT(*) as count FROM $table");
112 1
        $stmt->execute();
113 1
        $record = $stmt->fetch(PDO::FETCH_ASSOC);
114 1
        return (int) $record["count"];
115
    }
116
117 1
    public function findByKey(int $key)
118
    {
119 1
        $conn = $this->db();
120 1
        $sql = sprintf("SELECT * FROM %s WHERE %s = ?", $this->getTable(), $this->getPrimaryKey());
121 1
        $stmt = $conn->prepare($sql);
122 1
        $stmt->execute([$key]);
123 1
        return $stmt->fetch(PDO::FETCH_ASSOC);
124
    }
125
}