Completed
Push — master ( 1619eb...57b682 )
by Sebastian
05:03
created

DomainObjectAbstract   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 7
eloc 14
c 3
b 1
f 1
dl 0
loc 85
ccs 15
cts 15
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 4 2
A getId() 0 3 1
A setId() 0 9 2
A __isset() 0 7 2
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\DataMapper;
13
14
use UnexpectedValueException;
15
16
/**
17
 * Abstract Class for Domain Object.
18
 */
19
abstract class DomainObjectAbstract implements DomainObjectInterface
20
{
21
    /**
22
     * @var int Read only object id on persistent storage.
23
     */
24
    protected int $id = 0;
25
26
    /**
27
     * @var string Read only insertion date on persistent storage.
28
     */
29
    public string $created = '';
30
31
    /**
32
     * @var string Read only last update date on persistento storage.
33
     */
34
    public string $lastUpdate = '';
35
36
    /**
37
     * Get the id of the object (unique to the object type).
38
     *
39
     * <pre><code class="php">$object = new DomainObject($dependencies);
40
     *
41
     * $object->getId();
42
     * </code></pre>
43
     *
44
     * @return int Current object id.
45
     */
46 112
    public function getId(): int
47
    {
48 112
        return $this->id;
49
    }
50
51
    /**
52
     * Set the id for the object.
53
     *
54
     * <pre><code class="php">$object = new DomainObject($dependencies);
55
     *
56
     * $object->setId(5);
57
     * </code></pre>
58
     *
59
     * @param int $id New object id.
60
     *
61
     * @throws UnexpectedValueException If the id on the object is already set.
62
     *
63
     * @return int New object id.
64
     */
65 34
    public function setId(int $id): int
66
    {
67 34
        if ($this->id !== 0) {
68 1
            throw new UnexpectedValueException('ObjectId property is immutable.');
69
        }
70
71 33
        $this->id = $id;
72
73 33
        return $id;
74
    }
75
76
    /**
77
     * Return the value of a private or protected property.
78
     *
79
     * @param string $name
80
     *
81
     * @return mixed
82
     */
83 222
    public function __get(string $name)
84
    {
85 222
        if ($name === 'id') {
86 221
            return $this->id;
87
        }
88 1
    }
89
90
    /**
91
     * Return if a private or protected property exists.
92
     *
93
     * @param string $name
94
     *
95
     * @return bool
96
     */
97 220
    public function __isset(string $name)
98
    {
99 220
        if ($name === 'id') {
100 220
            return true;
101
        }
102
103 1
        return false;
104
    }
105
}
106