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

FloatValueObject   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 11
c 1
b 0
f 1
dl 0
loc 36
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getValue() 0 3 1
A equals() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TimiTao\ValueObject\Beberlei\Nullable;
6
7
use Exception;
8
use Throwable;
9
use TimiTao\ValueObject\Nullable\ValueObject\FloatValueObject as FloatValueObjectInterface;
10
11
abstract class FloatValueObject implements FloatValueObjectInterface
12
{
13
    private $value;
14
15
    /**
16
     * @throws Exception if value is invalid
17
     */
18
    public function __construct(?float $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(FloatValueObjectInterface $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(): ?float
37
    {
38
        return $this->value;
39
    }
40
41
    /**
42
     * @throws Throwable if value is invalid
43
     */
44
    abstract protected function guard(?float $value): void;
45
46
    abstract protected function throwException(?float $value, Throwable $e): Exception;
47
}
48