Completed
Push — main ( fffb13...d8afdd )
by Laurent
139:07 queued 124:29
created

Storage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 81
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromArray() 0 7 1
A unit() 0 8 2
A quantity() 0 8 2
A toArray() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Domain\Model\Article\VO;
6
7
use Domain\Model\Common\InvalidQuantity;
8
9
final class Storage
10
{
11
    public const UNITS = [
12
        'bouteille',
13
        'boite',
14
        'carton',
15
        'colis',
16
        'kilogramme',
17
        'litre',
18
        'pièce',
19
        'poche',
20
        'portion',
21
    ];
22
23
    /**
24
     * @var string
25
     */
26
    private $unit;
27
28
    /**
29
     * @var float
30
     */
31
    private $quantity;
32
33
    /**
34
     * Storage constructor.
35
     *
36
     * @param string $unit
37
     * @param float  $quantity
38
     */
39
    public function __construct(string $unit, float $quantity)
40
    {
41
        $this->unit = $unit;
42
        $this->quantity = $quantity;
43
    }
44
45
    public static function fromArray(array $storage): self
46
    {
47
        $unit = static::unit($storage[0]);
0 ignored issues
show
Comprehensibility introduced by
Since Domain\Model\Article\VO\Storage is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
48
        $quantity = static::quantity($storage[1]);
0 ignored issues
show
Comprehensibility introduced by
Since Domain\Model\Article\VO\Storage is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
49
50
        return new self($unit, $quantity);
51
    }
52
53
    /**
54
     * Test the unit.
55
     *
56
     * @param string $unit
57
     *
58
     * @return string
59
     */
60
    private static function unit(string $unit): string
61
    {
62
        if (!in_array(strtolower($unit), self::UNITS)) {
63
            throw new InvalidUnit();
64
        }
65
66
        return strtolower($unit);
67
    }
68
69
    /**
70
     * Test the quantity.
71
     *
72
     * @param float $quantity
73
     *
74
     * @return float
75
     */
76
    private static function quantity(float $quantity): float
77
    {
78
        if (!is_float($quantity)) {
79
            throw new InvalidQuantity();
80
        }
81
82
        return $quantity;
83
    }
84
85
    public function toArray(): array
86
    {
87
        return [$this->unit, $this->quantity];
88
    }
89
}
90