Passed
Push — master ( dbe76d...f2eeea )
by Aleksandr
30:21
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
}