Total Complexity | 7 |
Total Lines | 79 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
1 | <?php |
||
19 | abstract class DomainObjectAbstract implements DomainObjectInterface |
||
20 | { |
||
21 | use DomainObjectTimeTrait; |
||
22 | |||
23 | /** |
||
24 | * @var int Read only object id on persistent storage. |
||
25 | * |
||
26 | * -1 means that the id is not set! |
||
27 | */ |
||
28 | protected int $id = -1; |
||
29 | |||
30 | /** |
||
31 | * Get the id of the object (unique to the object type). |
||
32 | * |
||
33 | * <pre><code class="php">$object = new DomainObject($dependencies); |
||
34 | * |
||
35 | * $object->getId(); |
||
36 | * </code></pre> |
||
37 | * |
||
38 | * @return int Current object id. |
||
39 | */ |
||
40 | 112 | public function getId(): int |
|
41 | { |
||
42 | 112 | return $this->id; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Set the id for the object. |
||
47 | * |
||
48 | * <pre><code class="php">$object = new DomainObject($dependencies); |
||
49 | * |
||
50 | * $object->setId(5); |
||
51 | * </code></pre> |
||
52 | * |
||
53 | * @param int $id New object id. |
||
54 | * |
||
55 | * @throws UnexpectedValueException If the id on the object is already set. |
||
56 | * |
||
57 | * @return int New object id. |
||
58 | */ |
||
59 | 34 | public function setId(int $id): int |
|
60 | { |
||
61 | 34 | if ($this->id !== -1) { |
|
62 | 1 | throw new UnexpectedValueException('ObjectId property is immutable.'); |
|
63 | } |
||
64 | |||
65 | 33 | $this->id = $id; |
|
66 | |||
67 | 33 | return $id; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Return the value of a private or protected property. |
||
72 | * |
||
73 | * @param string $name |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 225 | public function __get(string $name) |
|
78 | { |
||
79 | 225 | if ($name === 'id') { |
|
80 | 224 | return $this->id; |
|
81 | } |
||
82 | 1 | } |
|
83 | |||
84 | /** |
||
85 | * Return if a private or protected property exists. |
||
86 | * |
||
87 | * @param string $name |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | 220 | public function __isset(string $name) |
|
98 | } |
||
99 | } |
||
100 |