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

AttributeGroupModel   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 35
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkGroupInAttributeSet() 0 17 1
A create() 0 7 1
A __construct() 0 4 1
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 Kuperwood\Eav\Enum\_GROUP;
13
use PDO;
14
15
class AttributeGroupModel extends Model
16
{
17 1
    public function __construct()
18
    {
19 1
        $this->setTable(_GROUP::table());
20 1
        $this->setPrimaryKey(_GROUP::ID);
21
    }
22
23
24 1
    public function create(array $data) : int
25
    {
26 1
        $conn = $this->getNativeConnection();
27 1
        $sql = sprintf("INSERT INTO %s (%s, %s) values(:sid, :name)", _GROUP::table(), _GROUP::SET_ID, _GROUP::NAME);
28 1
        $stmt = $conn->prepare($sql);
29 1
        $stmt->execute(['sid' => $data[_GROUP::SET_ID], 'name' => $data[_GROUP::NAME]]);
30 1
        return (int) $conn->lastInsertId();
31
    }
32
33 1
    public function checkGroupInAttributeSet(int $setKey, int $groupKey) : bool
34
    {
35 1
        $conn = $this->db();
36 1
        $sql = sprintf(
37 1
            "SELECT %s FROM %s WHERE %s = :set_id AND %s = :group_id",
38 1
            $this->getPrimaryKey(),
39 1
            $this->getTable(),
40 1
            _GROUP::SET_ID,
41 1
            _GROUP::ID
42 1
        );
43 1
        $stmt = $conn->prepare($sql);
44 1
        $stmt->bindParam(':set_id', $setKey, PDO::PARAM_INT);
45 1
        $stmt->bindParam(':group_id', $groupKey, PDO::PARAM_INT);
46 1
        $stmt->execute();
47
48 1
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
49 1
        return $result !== false;
50
    }
51
}