CartItemQuantity::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace SyliusCart\Domain\ValueObject;
6
7
use SyliusCart\Domain\Exception\InvalidCartItemQuantityException;
8
9
/**
10
 * @author Arkadiusz Krakowiak <[email protected]>
11
 */
12
final class CartItemQuantity
13
{
14
    /**
15
     * @var int
16
     */
17
    private $number;
18
19
    /**
20
     * @param int $number
21
     */
22
    private function __construct(int $number)
23
    {
24
        $this->number = $number;
25
    }
26
27
    /**
28
     * @param int $number
29
     *
30
     * @return CartItemQuantity
31
     */
32
    public static function create(int $number): self
33
    {
34
        if (0 >= $number) {
35
            throw new InvalidCartItemQuantityException('Cart item quantity cannot be equals or below zero.');
36
        }
37
38
        return new self($number);
39
    }
40
41
    /**
42
     * @param CartItemQuantity $quantity
43
     *
44
     * @return CartItemQuantity
45
     */
46
    public function add(CartItemQuantity $quantity): self
47
    {
48
        $number = $this->number + $quantity->getNumber();
49
50
        return CartItemQuantity::create($number);
51
    }
52
53
    /**
54
     * @param CartItemQuantity $quantity
55
     *
56
     * @return CartItemQuantity
57
     */
58
    public function subtract(CartItemQuantity $quantity): self
59
    {
60
        $number = $this->number - $quantity->getNumber();
61
62
        return CartItemQuantity::create($number);
63
    }
64
65
    /**
66
     * @param CartItemQuantity $quantity
67
     *
68
     * @return bool
69
     */
70
    public function equals(CartItemQuantity $quantity): bool
71
    {
72
        return $quantity->getNumber() === $this->number;
73
    }
74
75
    /**
76
     * @param CartItemQuantity $quantity
77
     *
78
     * @return bool
79
     */
80
    public function isHigherThan(CartItemQuantity $quantity): bool
81
    {
82
        return $quantity->getNumber() < $this->number;
83
    }
84
85
    /**
86
     * @param CartItemQuantity $quantity
87
     *
88
     * @return bool
89
     */
90
    public function isLowerThan(CartItemQuantity $quantity): bool
91
    {
92
        return $quantity->getNumber() > $this->number;
93
    }
94
95
    /**
96
     * @return int
97
     */
98
    public function getNumber(): int
99
    {
100
        return $this->number;
101
    }
102
}
103