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

ValueBase   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 89
c 0
b 0
f 0
dl 0
loc 132
ccs 100
cts 100
cp 1
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 11 1
B bulkCreate() 0 53 7
A __construct() 0 3 1
A update() 0 17 1
A find() 0 16 1
A create() 0 18 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 Drobotik\Eav\Enum\_VALUE;
14
use Drobotik\Eav\Enum\ATTR_TYPE;
15
use Drobotik\Eav\Import\Content\ValueSet;
16
use Drobotik\Eav\Trait\SingletonsTrait;
17
18
class ValueBase extends Model
19
{
20
21
    use SingletonsTrait;
22 1
    public function __construct()
23
    {
24 1
        $this->setPrimaryKey(_VALUE::ID->column());
25
    }
26
27 1
    public function find(string $table, int $domainKey, int $entityKey, int $attributeKey) : bool|array
28
    {
29 1
        return $this->db()
30 1
            ->createQueryBuilder()
31 1
            ->select('*')
32 1
            ->from($table)
33 1
            ->where(sprintf('%s = :domain AND %s = :entity AND %s = :attr',
34 1
                _VALUE::DOMAIN_ID->column(), _VALUE::ENTITY_ID->column(), _VALUE::ATTRIBUTE_ID->column()
35 1
            ))
36 1
            ->setParameters([
37 1
                "domain" => $domainKey,
38 1
                "entity" => $entityKey,
39 1
                "attr" => $attributeKey
40 1
            ])
41 1
            ->executeQuery()
42 1
            ->fetchAssociative();
43
    }
44
45 1
    public function create(string $table, int $domainKey, int $entityKey, int $attributeKey, $value) : int
46
    {
47 1
        $conn = $this->db();
48
49 1
        $conn->createQueryBuilder()
50 1
            ->insert($table)
51 1
            ->values([
52 1
                _VALUE::DOMAIN_ID->column() => '?',
53 1
                _VALUE::ENTITY_ID->column() => '?',
54 1
                _VALUE::ATTRIBUTE_ID->column() => '?',
55 1
                _VALUE::VALUE->column() => '?',
56 1
            ])
57 1
            ->setParameter(0, $domainKey)
58 1
            ->setParameter(1, $entityKey)
59 1
            ->setParameter(2, $attributeKey)
60 1
            ->setParameter(3, $value)
61 1
            ->executeQuery();
62 1
        return (int) $conn->lastInsertId();
63
    }
64
65 1
    public function update(string $table, int $domainKey, int $entityKey, int $attributeKey, $value) : int
66
    {
67 1
        $conn = $this->db();
68 1
        return $conn->createQueryBuilder()
69 1
            ->update($table)
70 1
            ->where(sprintf('%s = :domain AND %s = :entity AND %s = :attr',
71 1
                _VALUE::DOMAIN_ID->column(), _VALUE::ENTITY_ID->column(), _VALUE::ATTRIBUTE_ID->column()
72 1
            ))
73 1
            ->set(_VALUE::VALUE->column(), ':value')
74 1
            ->setParameters([
75 1
                "domain" => $domainKey,
76 1
                "entity" => $entityKey,
77 1
                "attr" => $attributeKey,
78 1
                "value" => $value
79 1
            ])
80 1
            ->executeQuery()
81 1
            ->rowCount();
82
    }
83
84 1
    public function destroy(string $table, int $domainKey, int $entityKey, int $attributeKey) : int
85
    {
86 1
        return $this->db()
87 1
            ->createQueryBuilder()
88 1
            ->delete($table)
89 1
            ->where(sprintf('%s = ? AND %s = ? AND %s = ?',
90 1
                _VALUE::DOMAIN_ID->column(), _VALUE::ENTITY_ID->column(), _VALUE::ATTRIBUTE_ID->column()
91 1
            ))
92 1
            ->setParameters([$domainKey, $entityKey, $attributeKey])
93 1
            ->executeQuery()
94 1
            ->rowCount();
95
    }
96
97 1
    public function bulkCreate(ValueSet $valueSet, int $domainKey): void
98
    {
99 1
        $pdo = Connection::get()->getNativeConnection();
100
101 1
        $template = "INSERT INTO %s ("._VALUE::DOMAIN_ID->column().","._VALUE::ENTITY_ID->column().","._VALUE::ATTRIBUTE_ID->column().","._VALUE::VALUE->column().")";
102
103 1
        $stringTable = ATTR_TYPE::STRING->valueTable();
104 1
        $integerTable = ATTR_TYPE::INTEGER->valueTable();
105 1
        $decimalTable = ATTR_TYPE::DECIMAL->valueTable();
106 1
        $datetimeTable = ATTR_TYPE::DATETIME->valueTable();
107 1
        $textTable = ATTR_TYPE::TEXT->valueTable();
108
109 1
        $stringTemplate = sprintf($template, $stringTable) . " VALUES %s;";
110 1
        $integerTemplate = sprintf($template, $integerTable) . " VALUES %s;";
111 1
        $decimalTemplate = sprintf($template, $decimalTable) . " VALUES %s;";
112 1
        $datetimeTemplate = sprintf($template, $datetimeTable) . " VALUES %s;";
113 1
        $textTemplate = sprintf($template, $textTable) . " VALUES %s;";
114
115 1
        $stringBulk = [];
116 1
        $integerBulk = [];
117 1
        $decimalBulk = [];
118 1
        $datetimeBulk = [];
119 1
        $textBulk = [];
120
121 1
        foreach ($valueSet->forExistingEntities() as $data) {
122 1
            $attributeKey = $data->getAttributeKey();
123 1
            $value = $data->getValue();
124 1
            $table = $data->getType()->valueTable();
125 1
            $entityKey = $data->getEntityKey();
126 1
            $bulkTemplate = "($domainKey, $entityKey, $attributeKey, '$value')";
127 1
            match($table) {
128 1
                $stringTable => $stringBulk[] = $bulkTemplate,
129 1
                $integerTable => $integerBulk[] = $bulkTemplate,
130 1
                $decimalTable => $decimalBulk[] = $bulkTemplate,
131 1
                $datetimeTable=> $datetimeBulk[] = $bulkTemplate,
132 1
                $textTable    => $textBulk[] = $bulkTemplate
133 1
            };
134
        }
135
136 1
        if(count($stringBulk) > 0) {
137 1
            $pdo->exec(sprintf($stringTemplate, implode(',', $stringBulk)));
138
        }
139 1
        if(count($integerBulk) > 0) {
140 1
            $pdo->exec(sprintf($integerTemplate, implode(',', $integerBulk)));
141
        }
142 1
        if(count($decimalBulk) > 0) {
143 1
            $pdo->exec(sprintf($decimalTemplate, implode(',', $decimalBulk)));
144
        }
145 1
        if(count($datetimeBulk) > 0) {
146 1
            $pdo->exec(sprintf($datetimeTemplate, implode(',', $datetimeBulk)));
147
        }
148 1
        if(count($textBulk) > 0) {
149 1
            $pdo->exec(sprintf($textTemplate, implode(',', $textBulk)));
150
        }
151
    }
152
153
}