Passed
Push — master ( c062f3...83edb5 )
by Aleksandr
02:32
created

AttributeSetModel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 46
ccs 29
cts 32
cp 0.9063
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 13 1
A findAttributes() 0 20 2
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\_ATTR;
14
use Drobotik\Eav\Enum\_PIVOT;
15
use Drobotik\Eav\Enum\_SET;
16
17
class AttributeSetModel extends Model
18
{
19
    public function __construct()
20
    {
21
        $this->setTable(_SET::table());
22
        $this->setPrimaryKey(_SET::ID->column());
23
    }
24
25 1
    public function create(array $data) : int
26
    {
27 1
        $conn = $this->db();
28 1
        $conn->createQueryBuilder()
29 1
            ->insert($this->getTable())
30 1
            ->values([
31 1
                _SET::DOMAIN_ID->column() => '?',
32 1
                _SET::NAME->column() => '?'
33 1
            ])
34 1
            ->setParameter(0, $data[_SET::DOMAIN_ID->column()])
35 1
            ->setParameter(1, $data[_SET::NAME->column()])
36 1
            ->executeQuery();
37 1
        return (int) $conn->lastInsertId();
38
    }
39
40
    /**
41
     * @throws Exception
42
     */
43 1
    public function findAttributes(int $domainKey, int $setKey = null) : array
44
    {
45 1
        $query = $this->db()
46 1
            ->createQueryBuilder()
47 1
            ->select('a.*')
48 1
            ->from(_ATTR::table(), 'a')
49 1
            ->innerJoin('a', _PIVOT::table(), 'p',
50 1
                sprintf('a.%s = p.%s', _ATTR::ID->column(), _PIVOT::ATTR_ID->column())
51 1
            )
52 1
            ->where(sprintf('p.%s = ?', _PIVOT::DOMAIN_ID->column()))
53 1
            ->setParameter(0, $domainKey);
54
55 1
        if(!is_null($setKey))
56 1
            $query = $query
57 1
                ->andWhere(sprintf('p.%s = ?', _PIVOT::SET_ID->column()))
58 1
                ->setParameter(1, $setKey);
59
60 1
        return $query
61 1
            ->executeQuery()
62 1
            ->fetchAllAssociative();
63
    }
64
}