ValueBase::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Alex Kuperwood <[email protected]>
5
 * @copyright 2025 Alex Kuperwood
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
declare(strict_types=1);
9
10
namespace Kuperwood\Eav\Model;
11
12
use Kuperwood\Eav\Database\Connection;
13
use Kuperwood\Eav\Enum\_VALUE;
14
use Kuperwood\Eav\Enum\ATTR_TYPE;
15
use Kuperwood\Eav\Traits\SingletonsTrait;
16
use Exception;
17
use PDO;
18
19
class ValueBase extends Model
20
{
21
22
    use SingletonsTrait;
23
    public function __construct()
24
    {
25 1
        $this->setPrimaryKey(_VALUE::ID);
26
    }
27 1
28
    public function find(string $type, $domainKey, $entityKey, $attributeKey)
29
    {
30 1
        $table = ATTR_TYPE::valueTable($type);
31
32 1
        $conn = $this->db();
33
        $sql = sprintf(
34 1
            "SELECT * FROM %s WHERE %s = :domain AND %s = :entity AND %s = :attr",
35 1
            $table,
36 1
            _VALUE::DOMAIN_ID,
37 1
            _VALUE::ENTITY_ID,
38 1
            _VALUE::ATTRIBUTE_ID
39 1
        );
40 1
41 1
        $stmt = $conn->prepare($sql);
42
        $stmt->bindParam(':domain', $domainKey, PDO::PARAM_INT);
43 1
        $stmt->bindParam(':entity', $entityKey, PDO::PARAM_INT);
44 1
        $stmt->bindParam(':attr', $attributeKey, PDO::PARAM_INT);
45 1
        $stmt->execute();
46 1
47 1
        $record = $stmt->fetch(PDO::FETCH_ASSOC);
48
        if ($record)
49 1
            $record[_VALUE::VALUE] = $this->makeValueParser()->parse($type, $record[_VALUE::VALUE]);
50
51
        return $record;
52
    }
53
54
    /**
55 1
     * @throws Exception
56
     */
57 1
    public function create(string $type, $domainKey, $entityKey, $attributeKey, $value) : int
58
    {
59 1
        $table = ATTR_TYPE::valueTable($type);
60 1
61 1
        $conn = $this->db();
62 1
        $sql = sprintf(
63 1
            "INSERT INTO %s (%s, %s, %s, %s) VALUES (:domain_id, :entity_id, :attribute_id, :value)",
64 1
            $table,
65 1
            _VALUE::DOMAIN_ID,
66 1
            _VALUE::ENTITY_ID,
67 1
            _VALUE::ATTRIBUTE_ID,
68
            _VALUE::VALUE
69 1
        );
70 1
71 1
        $stmt = $conn->prepare($sql);
72 1
        $stmt->bindParam(':domain_id', $domainKey, PDO::PARAM_INT);
73 1
        $stmt->bindParam(':entity_id', $entityKey, PDO::PARAM_INT);
74 1
        $stmt->bindParam(':attribute_id', $attributeKey, PDO::PARAM_INT);
75
        $stmt->bindParam(':value',$value);
76 1
77
        $stmt->execute();
78 1
79
        return (int) $conn->lastInsertId();
80
    }
81 1
82
    public function update(string $type, $domainKey, $entityKey, $attributeKey, $value) : int
83 1
    {
84 1
        $pdo = Connection::get();
85 1
        $table = ATTR_TYPE::valueTable($type);
86
87 1
        $sql = "UPDATE $table 
88 1
            SET " . _VALUE::VALUE . " = :val 
89 1
            WHERE " . _VALUE::DOMAIN_ID . " = :domain 
90 1
            AND " . _VALUE::ENTITY_ID . " = :entity 
91 1
            AND " . _VALUE::ATTRIBUTE_ID . " = :attr";
92
93 1
        $stmt = $pdo->prepare($sql);
94 1
        $stmt->bindParam(':val', $value);
95 1
        $stmt->bindParam(':domain', $domainKey, PDO::PARAM_INT);
96 1
        $stmt->bindParam(':entity', $entityKey, PDO::PARAM_INT);
97 1
        $stmt->bindParam(':attr', $attributeKey, PDO::PARAM_INT);
98 1
        $stmt->execute();
99
100 1
        return $stmt->rowCount();
101
    }
102
103 1
    public function destroy(string $type, $domainKey, $entityKey, $attributeKey) : int
104
    {
105 1
        $pdo = Connection::get();
106 1
        $table = ATTR_TYPE::valueTable($type);
107
108 1
        $sql = "DELETE FROM $table 
109 1
            WHERE " . _VALUE::DOMAIN_ID . " = :domain 
110 1
            AND " . _VALUE::ENTITY_ID . " = :entity 
111 1
            AND " . _VALUE::ATTRIBUTE_ID . " = :attr";
112
113 1
        $stmt = $pdo->prepare($sql);
114 1
        $stmt->bindParam(':domain', $domainKey, PDO::PARAM_INT);
115 1
        $stmt->bindParam(':entity', $entityKey, PDO::PARAM_INT);
116 1
        $stmt->bindParam(':attr', $attributeKey, PDO::PARAM_INT);
117 1
        $stmt->execute();
118
119 1
        return $stmt->rowCount();
120
    }
121
122
}