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

Entity   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 105
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 105
ccs 39
cts 39
cp 1
rs 10
wmc 20

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getBag() 0 3 1
A getAttributeSet() 0 3 1
A setAttributeSet() 0 6 1
A find() 0 3 1
A hasKey() 0 3 2
A getGnome() 0 3 1
A __construct() 0 6 1
A delete() 0 3 1
A hasDomainKey() 0 3 2
A findByKey() 0 7 2
A getDomainKey() 0 3 1
A toArray() 0 3 1
A validate() 0 3 1
A setDomainKey() 0 5 1
A save() 0 3 1
A getKey() 0 3 1
A setKey() 0 5 1
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 *
5
 * @author    Alex Kuperwood <[email protected]>
6
 * @copyright 2025 Alex Kuperwood
7
 * @license   https://opensource.org/license/mit  The MIT License
8
 */
9
declare(strict_types=1);
10
11
namespace Kuperwood\Eav;
12
13
use Kuperwood\Eav\Enum\_RESULT;
14
use Kuperwood\Eav\Result\Result;
15
16
class Entity
17
{
18
    private $key;
19
    private $domainKey;
20
    private AttributeSet $attributeSet;
21
    private EntityBag $bag;
22
    private EntityGnome $manager;
23 3
24
    public function __construct()
25 3
    {
26 3
        $this->bag = (new EntityBag())->setEntity($this);
27 3
        $this->attributeSet = new AttributeSet();
28 3
        $this->attributeSet->setEntity($this);
29
        $this->manager = new EntityGnome($this);
30
    }
31 2
32
    public function getKey(): int
33 2
    {
34
        return $this->key;
35
    }
36 2
37
    public function setKey(int $key): self
38 2
    {
39
        $this->key = $key;
40 2
41
        return $this;
42
    }
43 2
44
    public function hasKey(): bool
45 2
    {
46
        return isset($this->key) && 0 !== $this->key;
47
    }
48 2
49
    public function setDomainKey($key): self
50 2
    {
51
        $this->domainKey = $key;
52 2
53
        return $this;
54
    }
55 2
56
    public function getDomainKey()
57 2
    {
58
        return $this->domainKey;
59
    }
60 2
61
    public function hasDomainKey(): bool
62 2
    {
63
        return isset($this->domainKey) && 0 !== $this->domainKey;
64
    }
65 1
66
    public function getGnome(): EntityGnome
67 1
    {
68
        return $this->manager;
69
    }
70 1
71
    public function getAttributeSet(): ?AttributeSet
72 1
    {
73
        return $this->attributeSet;
74
    }
75 1
76
    public function setAttributeSet(AttributeSet $attributeSet): self
77 1
    {
78 1
        $attributeSet->setEntity($this);
79
        $this->attributeSet = $attributeSet;
80 1
81
        return $this;
82
    }
83 1
84
    public function getBag(): EntityBag
85 1
    {
86
        return $this->bag;
87
    }
88 1
89
    public function find(): Result
90 1
    {
91
        return $this->getGnome()->find();
92
    }
93 3
94
    public function save(): Result
95 3
    {
96
        return $this->getGnome()->save();
97
    }
98 3
99
    public function delete(): Result
100 3
    {
101
        return $this->getGnome()->delete();
102
    }
103 1
104
    public function validate(): Result
105 1
    {
106
        return $this->getGnome()->validate();
107
    }
108 1
109
    public function toArray(): array
110 1
    {
111
        return $this->getGnome()->toArray();
112
    }
113
114
    public static function findByKey($entityKey) : ?Entity
115
    {
116
        $entity = new self();
117
        $entity->setKey($entityKey);
118
        if ($entity->find()->getCode() === _RESULT::FOUND)
119
            return $entity;
120
        return null;
121
    }
122
}
123