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

PivotModel   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 51
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0
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 Doctrine\DBAL\Exception;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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