Passed
Push — master ( 3c9f2b...89e5a5 )
by Aleksandr
02:55
created

EntityModel   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
eloc 51
c 0
b 0
f 0
dl 0
loc 100
ccs 50
cts 58
cp 0.8621
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isServiceKey() 0 14 1
A create() 0 13 1
A getBySetAndDomain() 0 9 1
A getServiceKey() 0 6 2
A getByServiceKey() 0 12 1
A bulkCreate() 0 19 3
A __construct() 0 4 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 Doctrine\DBAL\Exception;
13
use Drobotik\Eav\Enum\_ENTITY;
14
use Drobotik\Eav\Exception\EntityException;
15
use Drobotik\Eav\Trait\SingletonsTrait;
16
use PDO;
17
18
class EntityModel extends Model
19
{
20
    use SingletonsTrait;
21
22
23 1
    public function __construct()
24
    {
25 1
        $this->setTable(_ENTITY::table());
26 1
        $this->setKeyName(_ENTITY::ID->column());
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
        $conn->createQueryBuilder()
44 1
            ->insert($this->getTable())
45 1
            ->values([
46 1
                _ENTITY::DOMAIN_ID->column() => '?',
47 1
                _ENTITY::ATTR_SET_ID->column() => '?'
48 1
            ])
49 1
            ->setParameter(0, $data[_ENTITY::DOMAIN_ID->column()])
50 1
            ->setParameter(1, $data[_ENTITY::ATTR_SET_ID->column()])
51 1
            ->executeQuery();
52 1
        return (int) $conn->lastInsertId();
53
    }
54
55 1
    public function isServiceKey(int $key) : bool
56
    {
57 1
        $table = $this->getTable();
58 1
        $serviceKeyCol = _ENTITY::SERVICE_KEY->column();
59
60 1
        $conn = $this->db()->getNativeConnection();
61
62 1
        $stmt = $conn->prepare("SELECT count(*) as c FROM $table WHERE $serviceKeyCol = :key");
63 1
        $stmt->bindParam(':key', $key);
64
65 1
        $stmt->execute();
66 1
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
67
68 1
        return (int) $result['c'] > 0;
69
    }
70
71 1
    public function getByServiceKey(int $key): bool|array
72
    {
73 1
        $table = $this->getTable();
74 1
        $serviceKeyCol = _ENTITY::SERVICE_KEY->column();
75
76 1
        $conn = $this->db()->getNativeConnection();;
77
78 1
        $stmt = $conn->prepare("SELECT * FROM $table WHERE $serviceKeyCol = :key");
79 1
        $stmt->bindParam(':key', $key);
80
81 1
        $stmt->execute();
82 1
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
83
    }
84
85
    /**
86
     * @throws EntityException
87
     */
88 2
    public function bulkCreate(int $amount, int $domainKey, int $setKey, int $serviceKey) : void
89
    {
90 2
        if ($amount < 1)
91
        {
92 1
            EntityException::mustBePositiveAmount();
93
        }
94 1
        $format = "($domainKey, $setKey, $serviceKey)";
95 1
        $bulk = [];
96 1
        for($i=0;$i<$amount;$i++) {
97 1
            $bulk[] = $format;
98
        }
99 1
        $template = sprintf(
100 1
            "INSERT INTO "._ENTITY::table()." ("._ENTITY::DOMAIN_ID->column().", "._ENTITY::ATTR_SET_ID->column().", "._ENTITY::SERVICE_KEY->column().") VALUES %s;",
101 1
            implode(',',$bulk)
102 1
        );
103
104 1
        $conn = $this->db()->getNativeConnection();
105
106 1
        $conn->exec($template);
107
    }
108
109
    public function getBySetAndDomain(int $domainKey, int $setKey) : array
110
    {
111
        return $this->db()->createQueryBuilder()
112
            ->select('*')
113
            ->from($this->getTable())
114
            ->where(sprintf('%s = %s', _ENTITY::DOMAIN_ID->column(), $domainKey))
115
            ->andWhere(sprintf('%s = %s', _ENTITY::ATTR_SET_ID->column(), $setKey))
116
            ->executeQuery()
117
            ->fetchAllAssociative();
118
    }
119
}