Passed
Push — master ( 17e6cc...350d8b )
by Aleksandr
41:29 queued 06:24
created

EntityModel::getBySetAndDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 15
ccs 10
cts 10
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    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 Doctrine\DBAL\Exception;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Exception 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...
13
use Kuperwood\Eav\Database\Connection;
14
use Kuperwood\Eav\Enum\_ENTITY;
15
use Kuperwood\Eav\Exception\EntityException;
16
use Kuperwood\Eav\Traits\SingletonsTrait;
17
use PDO;
18
19
class EntityModel extends Model
20
{
21
    use SingletonsTrait;
22
23 1
    public function __construct()
24
    {
25 1
        $this->setTable(_ENTITY::table());
26 1
        $this->setPrimaryKey(_ENTITY::ID);
27
    }
28
29 2
    public function getServiceKey(): int
30
    {
31 2
        $key = $this->makeFakerGenerator()->randomDigit();
32 2
        return $this->isServiceKey($key)
33 1
            ? $this->getServiceKey()
34 2
            : $key;
35
    }
36
37
    /**
38
     * @throws Exception
39
     */
40 1
    public function create(array $data) : int
41
    {
42 1
        $conn = $this->db();
43 1
        $sql = sprintf(
44 1
            "INSERT INTO %s (%s, %s) VALUES (:domain_id, :attr_set_id)",
45 1
            $this->getTable(),
46 1
            _ENTITY::DOMAIN_ID,
47 1
            _ENTITY::ATTR_SET_ID
48 1
        );
49 1
        $stmt = $conn->prepare($sql);
50 1
        $stmt->bindParam(':domain_id', $data[_ENTITY::DOMAIN_ID]);
51 1
        $stmt->bindParam(':attr_set_id', $data[_ENTITY::ATTR_SET_ID]);
52 1
        $stmt->execute();
53 1
        return (int) $conn->lastInsertId();
54
    }
55
56 1
    public function isServiceKey(int $key) : bool
57
    {
58 1
        $table = $this->getTable();
59 1
        $serviceKeyCol = _ENTITY::SERVICE_KEY;
60
61 1
        $conn = $this->db();
62
63 1
        $stmt = $conn->prepare("SELECT count(*) as c FROM $table WHERE $serviceKeyCol = :key");
64 1
        $stmt->bindParam(':key', $key);
65
66 1
        $stmt->execute();
67 1
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
68
69 1
        return (int) $result['c'] > 0;
70
    }
71
72 1
    public function getByServiceKey(int $key)
73
    {
74 1
        $table = $this->getTable();
75 1
        $serviceKeyCol = _ENTITY::SERVICE_KEY;
76
77 1
        $conn = $this->db();
78
79 1
        $stmt = $conn->prepare("SELECT * FROM $table WHERE $serviceKeyCol = :key");
80 1
        $stmt->bindParam(':key', $key);
81
82 1
        $stmt->execute();
83 1
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
84
    }
85
86
    /**
87
     * @throws EntityException
88
     */
89 2
    public function bulkCreate(int $amount, int $domainKey, int $setKey, int $serviceKey) : void
90
    {
91 2
        if ($amount < 1)
92
        {
93 1
            EntityException::mustBePositiveAmount();
94
        }
95 1
        $format = "($domainKey, $setKey, $serviceKey)";
96 1
        $bulk = [];
97 1
        for($i=0;$i<$amount;$i++) {
98 1
            $bulk[] = $format;
99
        }
100 1
        $template = sprintf(
101 1
            "INSERT INTO "._ENTITY::table()." ("._ENTITY::DOMAIN_ID.", "._ENTITY::ATTR_SET_ID.", "._ENTITY::SERVICE_KEY.") VALUES %s;",
102 1
            implode(',',$bulk)
103 1
        );
104
105 1
        $conn = $this->db();
106
107 1
        $conn->exec($template);
108
    }
109
110 1
    public function getBySetAndDomain(int $domainKey, int $setKey) : array
111
    {
112 1
        $conn = Connection::get();
113 1
        $table = $this->getTable();
114
115 1
        $stmt = $conn->prepare(
116 1
            "SELECT * FROM $table WHERE " . _ENTITY::DOMAIN_ID . " = :domainKey AND " . _ENTITY::ATTR_SET_ID . " = :setKey"
117 1
        );
118
119 1
        $stmt->bindParam(':domainKey', $domainKey, PDO::PARAM_INT);
120 1
        $stmt->bindParam(':setKey', $setKey, PDO::PARAM_INT);
121
122 1
        $stmt->execute();
123
124 1
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
125
    }
126
}