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

AttributeSetModel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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