Passed
Push — master ( 57eb13...500eb5 )
by Aleksandr
34:06
created

AttributeGroupModel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 49
ccs 24
cts 24
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkGroupInAttributeSet() 0 17 1
A create() 0 7 1
A __construct() 0 4 1
A findBySetKey() 0 13 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\Database\Connection;
13
use Kuperwood\Eav\Enum\_GROUP;
14
use PDO;
15
16
class AttributeGroupModel extends Model
17 1
{
18
    public function __construct()
19 1
    {
20 1
        $this->setTable(_GROUP::table());
21
        $this->setPrimaryKey(_GROUP::ID);
22
    }
23
24 1
    public function create(array $data) : int
25
    {
26 1
        $conn = Connection::get();
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
52
    public function findBySetKey($setKey)
53
    {
54
        $conn = $this->db();
55
        $sql = sprintf(
56
            "SELECT * FROM %s WHERE %s = :set_id ",
57
            $this->getTable(),
58
            _GROUP::SET_ID,
59
        );
60
        $stmt = $conn->prepare($sql);
61
        $stmt->bindParam(':set_id', $setKey, PDO::PARAM_INT);
62
        $stmt->execute();
63
64
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
65
    }
66
}