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

IntegerValueObject::equals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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