Passed
Push — master ( 2b5d19...008e72 )
by Tomasz
01:35
created

TimestampValueObject::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TimiTao\ValueObject\Beberlei\DateTime;
6
7
use DateTimeImmutable;
8
use Exception;
9
use Throwable;
10
use TimiTao\ValueObject\Core\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
        return $this->value;
32
    }
33
34
    public function equals(TimestampValueObjectInterface $other): bool
35
    {
36
        if (static::class !== get_class($other)) {
37
            return false;
38
        }
39
        return $this->getValue() === $other->getValue();
40
    }
41
42
    public function getValue(): int
43
    {
44
        return $this->value->getTimestamp();
45
    }
46
47
    /**
48
     * @throws Throwable if value is invalid
49
     */
50
    abstract protected function guard(DateTimeImmutable $value): void;
51
52
    abstract protected function throwException(DateTimeImmutable $value, Throwable $e): Exception;
53
}
54