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

ValueBase::bulkCreate()   C

Complexity

Conditions 12
Paths 193

Size

Total Lines 70
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 51
nc 193
nop 2
dl 0
loc 70
ccs 0
cts 50
cp 0
crap 156
rs 6.1916
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
0 ignored issues
show
Bug introduced by
The type Drobotik\Eav\Import\Content\ValueSet 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...
16
use Drobotik\Eav\Traits\SingletonsTrait;
17
use Exception;
18
use InvalidArgumentException;
19
use PDO;
20
21
class ValueBase extends Model
22
{
23
24
    use SingletonsTrait;
25 1
    public function __construct()
26
    {
27 1
        $this->setPrimaryKey(_VALUE::ID);
28
    }
29
30 1
    public function find(string $type, $domainKey, $entityKey, $attributeKey)
31
    {
32 1
        $table = ATTR_TYPE::valueTable($type);
33
34 1
        $conn = $this->db();
35 1
        $sql = sprintf(
36 1
            "SELECT * FROM %s WHERE %s = :domain AND %s = :entity AND %s = :attr",
37 1
            $table,
38 1
            _VALUE::DOMAIN_ID,
39 1
            _VALUE::ENTITY_ID,
40 1
            _VALUE::ATTRIBUTE_ID
41 1
        );
42
43 1
        $stmt = $conn->prepare($sql);
44 1
        $stmt->bindParam(':domain', $domainKey, PDO::PARAM_INT);
45 1
        $stmt->bindParam(':entity', $entityKey, PDO::PARAM_INT);
46 1
        $stmt->bindParam(':attr', $attributeKey, PDO::PARAM_INT);
47 1
        $stmt->execute();
48
49 1
        return $stmt->fetch(PDO::FETCH_ASSOC);
50
    }
51
52
    /**
53
     * @throws Exception
54
     */
55 1
    public function create(string $type, $domainKey, $entityKey, $attributeKey, $value) : int
56
    {
57 1
        $table = ATTR_TYPE::valueTable($type);
58
59 1
        $conn = $this->db();
60 1
        $sql = sprintf(
61 1
            "INSERT INTO %s (%s, %s, %s, %s) VALUES (:domain_id, :entity_id, :attribute_id, :value)",
62 1
            $table,
63 1
            _VALUE::DOMAIN_ID,
64 1
            _VALUE::ENTITY_ID,
65 1
            _VALUE::ATTRIBUTE_ID,
66 1
            _VALUE::VALUE
67 1
        );
68
69 1
        $stmt = $conn->prepare($sql);
70 1
        $stmt->bindParam(':domain_id', $domainKey, PDO::PARAM_INT);
71 1
        $stmt->bindParam(':entity_id', $entityKey, PDO::PARAM_INT);
72 1
        $stmt->bindParam(':attribute_id', $attributeKey, PDO::PARAM_INT);
73 1
        $v = $this->makeValueParser()->parse($type, $value);
74 1
        $stmt->bindParam(':value',$v);
75
76 1
        $stmt->execute();
77
78 1
        return (int) $conn->lastInsertId();
79
    }
80
81 1
    public function update(string $type, $domainKey, $entityKey, $attributeKey, $value) : int
82
    {
83 1
        $pdo = Connection::get();
84 1
        $table = ATTR_TYPE::valueTable($type);
85 1
        $parsedValue = $this->makeValueParser()->parse($type, $value);
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', $parsedValue);
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
    public function bulkCreate(ValueSet $valueSet, int $domainKey): void
123
    {
124
        $pdo = Connection::get();
125
126
        $template = "INSERT INTO %s ("._VALUE::DOMAIN_ID.","._VALUE::ENTITY_ID.","._VALUE::ATTRIBUTE_ID.","._VALUE::VALUE.")";
127
128
        $stringTable = ATTR_TYPE::valueTable(ATTR_TYPE::STRING);
129
        $integerTable = ATTR_TYPE::valueTable(ATTR_TYPE::INTEGER);
130
        $decimalTable = ATTR_TYPE::valueTable(ATTR_TYPE::DECIMAL);
131
        $datetimeTable = ATTR_TYPE::valueTable(ATTR_TYPE::DATETIME);
132
        $textTable = ATTR_TYPE::valueTable(ATTR_TYPE::TEXT);
133
134
        $stringTemplate = sprintf($template, $stringTable) . " VALUES %s;";
135
        $integerTemplate = sprintf($template, $integerTable) . " VALUES %s;";
136
        $decimalTemplate = sprintf($template, $decimalTable) . " VALUES %s;";
137
        $datetimeTemplate = sprintf($template, $datetimeTable) . " VALUES %s;";
138
        $textTemplate = sprintf($template, $textTable) . " VALUES %s;";
139
140
        $stringBulk = [];
141
        $integerBulk = [];
142
        $decimalBulk = [];
143
        $datetimeBulk = [];
144
        $textBulk = [];
145
146
        foreach ($valueSet->forExistingEntities() as $data) {
147
            $attributeKey = $data->getAttributeKey();
148
            $value = $data->getValue();
149
            $table = ATTR_TYPE::valueTable($data->getType());
150
            $entityKey = $data->getEntityKey();
151
            $bulkTemplate = "($domainKey, $entityKey, $attributeKey, '$value')";
152
            switch ($table) {
153
                case $stringTable:
154
                    $stringBulk[] = $bulkTemplate;
155
                    break;
156
157
                case $integerTable:
158
                    $integerBulk[] = $bulkTemplate;
159
                    break;
160
161
                case $decimalTable:
162
                    $decimalBulk[] = $bulkTemplate;
163
                    break;
164
165
                case $datetimeTable:
166
                    $datetimeBulk[] = $bulkTemplate;
167
                    break;
168
169
                case $textTable:
170
                    $textBulk[] = $bulkTemplate;
171
                    break;
172
173
                default:
174
                    throw new InvalidArgumentException("Unhandled table: " . $table);
175
            }
176
        }
177
178
        if(count($stringBulk) > 0) {
179
            $pdo->exec(sprintf($stringTemplate, implode(',', $stringBulk)));
180
        }
181
        if(count($integerBulk) > 0) {
182
            $pdo->exec(sprintf($integerTemplate, implode(',', $integerBulk)));
183
        }
184
        if(count($decimalBulk) > 0) {
185
            $pdo->exec(sprintf($decimalTemplate, implode(',', $decimalBulk)));
186
        }
187
        if(count($datetimeBulk) > 0) {
188
            $pdo->exec(sprintf($datetimeTemplate, implode(',', $datetimeBulk)));
189
        }
190
        if(count($textBulk) > 0) {
191
            $pdo->exec(sprintf($textTemplate, implode(',', $textBulk)));
192
        }
193
    }
194
195
}