Passed
Push — master ( 008e72...5c8957 )
by Tomasz
02:43
created

TimestampValueObject::equals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TimiTao\ValueObject\Beberlei\Nullable;
6
7
use DateTimeImmutable;
8
use Exception;
9
use Throwable;
10
use TimiTao\ValueObject\Nullable\DateTime\TimestampValueObject as TimestampValueObjectInterface;
11
12
abstract class TimestampValueObject implements TimestampValueObjectInterface
13
{
14
    private $value;
15
16
    /**
17
     * @throws Exception if value is invalid
18
     */
19
    public function __construct(?DateTimeImmutable $value)
20
    {
21
        try {
22
            $this->guard($value);
23
        } catch (Throwable $e) {
24
            throw $this->throwException($value, $e);
25
        }
26
        $this->value = $value;
27
    }
28
29
    public function getDateTime(): ?DateTimeImmutable
30
    {
31
        if ($this->value === null) {
32
            return null;
33
        }
34
        return $this->value;
35
    }
36
37
    public function equals(TimestampValueObjectInterface $other): bool
38
    {
39
        if (static::class !== get_class($other)) {
40
            return false;
41
        }
42
        return $this->getValue() === $other->getValue();
43
    }
44
45
    public function getValue(): ?int
46
    {
47
        return $this->value->getTimestamp();
48
    }
49
50
    /**
51
     * @throws Throwable if value is invalid
52
     */
53
    abstract protected function guard(?DateTimeImmutable $value): void;
54
55
    abstract protected function throwException(?DateTimeImmutable $value, Throwable $e): Exception;
56
}
57