Passed
Push — master ( ac7162...0cc260 )
by Aleksandr
02:52
created

AttributeGroupModel::checkGroupInAttributeSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 12
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
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\_GROUP;
14
use PDO;
15
16
class AttributeGroupModel extends Model
17
{
18 1
    public function __construct()
19
    {
20 1
        $this->setTable(_GROUP::table());
21 1
        $this->setPrimaryKey(_GROUP::ID->column());
22
    }
23
24
    /**
25
     * @throws Exception
26
     */
27 1
    public function create(array $data) : int
28
    {
29 1
        $conn = $this->db();
30 1
        $conn->createQueryBuilder()
31 1
            ->insert(_GROUP::table())
32 1
            ->values([
33 1
                _GROUP::SET_ID->column() => '?',
34 1
                _GROUP::NAME->column() => '?'
35 1
            ])
36 1
            ->setParameter(0, $data[_GROUP::SET_ID->column()])
37 1
            ->setParameter(1, $data[_GROUP::NAME->column()])
38 1
            ->executeQuery();
39 1
        return (int) $conn->lastInsertId();
40
    }
41
42 1
    public function checkGroupInAttributeSet(int $setKey, int $groupKey) : bool
43
    {
44 1
        $result = $this->db()->createQueryBuilder()
45 1
            ->select($this->getPrimaryKey())
46 1
            ->from($this->getTable())
47 1
            ->where(sprintf('%s = ?', _GROUP::SET_ID->column()))
48 1
            ->andWhere(sprintf('%s = ?', _GROUP::ID->column()))
49 1
            ->setParameter(0, $setKey, PDO::PARAM_INT)
50 1
            ->setParameter(1, $groupKey, PDO::PARAM_INT)
51 1
            ->executeQuery()
52 1
            ->fetchAssociative();
53 1
        return $result !== false;
54
    }
55
}