PivotModel   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 51
ccs 37
cts 37
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findOne() 0 20 1
A create() 0 18 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\_PIVOT;
13
use PDO;
14
15
class PivotModel extends Model
16
{
17
    public function __construct()
18 1
    {
19
        $this->setTable(_PIVOT::table());
20 1
        $this->setPrimaryKey(_PIVOT::ID);
21 1
    }
22
23
    /**
24
     * @throws Exception
25
     */
26
    public function create(array $data) : int
27 1
    {
28
        $conn = $this->db();
29 1
        $sql = sprintf(
30 1
            "INSERT INTO %s (%s, %s, %s, %s) VALUES (:domain_id, :set_id, :group_id, :attr_id)",
31 1
            $this->getTable(),
32 1
            _PIVOT::DOMAIN_ID,
33 1
            _PIVOT::SET_ID,
34 1
            _PIVOT::GROUP_ID,
35 1
            _PIVOT::ATTR_ID
36 1
        );
37 1
        $stmt = $conn->prepare($sql);
38 1
        $stmt->bindParam(':domain_id', $data[_PIVOT::DOMAIN_ID]);
39 1
        $stmt->bindParam(':set_id', $data[_PIVOT::SET_ID]);
40 1
        $stmt->bindParam(':group_id', $data[_PIVOT::GROUP_ID]);
41 1
        $stmt->bindParam(':attr_id', $data[_PIVOT::ATTR_ID]);
42 1
        $stmt->execute();
43 1
        return (int) $conn->lastInsertId();
44 1
    }
45
46
    public function findOne(int $domainKey, $setKey, $groupKey, $attributeKey)
47 1
    {
48
        $conn = $this->db();
49 1
        $sql = sprintf(
50 1
            "SELECT %s FROM %s WHERE %s = :domain_id AND %s = :set_id AND %s = :group_id AND %s = :attr_id",
51 1
            $this->getPrimaryKey(),
52 1
            $this->getTable(),
53 1
            _PIVOT::DOMAIN_ID,
54 1
            _PIVOT::SET_ID,
55 1
            _PIVOT::GROUP_ID,
56 1
            _PIVOT::ATTR_ID
57 1
        );
58 1
        $stmt = $conn->prepare($sql);
59 1
        $stmt->bindParam(':domain_id', $domainKey, PDO::PARAM_INT);
60 1
        $stmt->bindParam(':set_id', $setKey, PDO::PARAM_INT);
61 1
        $stmt->bindParam(':group_id', $groupKey, PDO::PARAM_INT);
62 1
        $stmt->bindParam(':attr_id', $attributeKey, PDO::PARAM_INT);
63 1
        $stmt->execute();
64 1
65
        return $stmt->fetch(PDO::FETCH_ASSOC);
66
    }
67
}