Passed
Push — develop ( 7d2e81...61e6f0 )
by Laurent
06:22 queued 03:53
created

Storage::isValidQuantity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Administration\Domain\Article\Model\VO;
15
16
use Core\Domain\Common\Model\Exception\InvalidQuantity;
17
18
final class Storage
19
{
20
    public const UNITS = [
21
        'bouteille',
22
        'boite',
23
        'carton',
24
        'colis',
25
        'kilogramme',
26
        'litre',
27
        'pièce',
28
        'poche',
29
        'portion',
30
    ];
31
32
    private string $unit;
33
    private float $quantity;
34
35
    public function __construct(string $unit, float $quantity)
36
    {
37
        $this->unit = $unit;
38
        $this->quantity = $quantity;
39
    }
40
41
    public static function fromArray(array $storage): self
42
    {
43
        $unit = static::isValidUnit($storage[0]);
44
        $quantity = static::isValidQuantity($storage[1]);
45
46
        return new self($unit, $quantity);
47
    }
48
49
    public function toArray(): array
50
    {
51
        return [$this->unit, $this->quantity];
52
    }
53
54
    private static function isValidUnit(string $unit): string
55
    {
56
        if (!\in_array(\strtolower($unit), self::UNITS, true)) {
57
            throw new InvalidUnit();
58
        }
59
60
        return \strtolower($unit);
61
    }
62
63
    private static function isValidQuantity(float $quantity): float
64
    {
65
        if (!\is_float($quantity)) {
0 ignored issues
show
introduced by
The condition is_float($quantity) is always true.
Loading history...
66
            throw new InvalidQuantity();
67
        }
68
69
        return $quantity;
70
    }
71
}
72