AttributeSetModel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 56
ccs 33
cts 33
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 14 1
A findAttributes() 0 30 3
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\_ATTR;
13
use Kuperwood\Eav\Enum\_PIVOT;
14
use Kuperwood\Eav\Enum\_SET;
15
use PDO;
16
17
class AttributeSetModel extends Model
18
{
19
    public function __construct()
20 1
    {
21
        $this->setTable(_SET::table());
22 1
        $this->setPrimaryKey(_SET::ID);
23 1
    }
24
25
    public function create(array $data) : int
26 1
    {
27
        $conn = $this->db();
28 1
        $sql = sprintf(
29 1
            "INSERT INTO %s (%s, %s) VALUES (:domain_id, :name)",
30 1
            $this->getTable(),
31 1
            _SET::DOMAIN_ID,
32 1
            _SET::NAME
33 1
        );
34 1
        $stmt = $conn->prepare($sql);
35 1
        $stmt->bindParam(':domain_id', $data[_SET::DOMAIN_ID]);
36 1
        $stmt->bindParam(':name', $data[_SET::NAME]);
37 1
        $stmt->execute();
38 1
        return (int) $conn->lastInsertId();
39 1
    }
40
    /**
41
     * @throws Exception
42
     */
43
    public function findAttributes($domainKey, $setKey = null) : array
44 1
    {
45
        $sql = sprintf(
46 1
            "SELECT a.*, p.%s FROM %s a
47 1
        INNER JOIN %s p ON a.%s = p.%s
48
        WHERE p.%s = :domain_id",
49 1
            _PIVOT::GROUP_ID,
50 1
            _ATTR::table(),
51 1
            _PIVOT::table(),
52 1
            _ATTR::ID,
53 1
            _PIVOT::ATTR_ID,
54
            _PIVOT::DOMAIN_ID
55 1
        );
56 1
57
        if (!is_null($setKey)) {
58
            $sql .= sprintf(" AND p.%s = :set_id", _PIVOT::SET_ID);
59 1
        }
60 1
61
        $conn = $this->db();
62 1
        $stmt = $conn->prepare($sql);
63
64 1
        $stmt->bindParam(':domain_id', $domainKey, PDO::PARAM_INT);
65 1
66
        if (!is_null($setKey)) {
67
            $stmt->bindParam(':set_id', $setKey, PDO::PARAM_INT);
68 1
        }
69
70 1
        $stmt->execute();
71
72
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
73
    }
74
}