Passed
Branch master (168fd2)
by Dariusz
06:34
created

CartItem::setPriceFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the Plane\Shop package.
5
 *
6
 * (c) Dariusz Korsak <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Plane\Shop;
13
14
use Money\Money;
15
use Plane\Shop\Validator\QuantityValidatorInterface;
16
use Plane\Shop\Exception\QuanityException;
17
18
class CartItem implements CartItemInterface
19
{
20
    private $product;
21
    
22
    private $quantityValidator;
23
    
24
    private $quantity;
25
    
26 42
    public function __construct(
27
        ProductInterface $product,
28
        $quantity = 1,
29
        QuantityValidatorInterface $quantityValidator = null
30
    ) {
31 42
        $this->product = $product;
32 42
        $this->quantityValidator = $quantityValidator;
33 42
        $this->setQuantity((int) $quantity);
34 41
    }
35
    
36 1
    public function getProduct(): ProductInterface
37
    {
38 1
        return $this->product;
39
    }
40
41 36
    public function getId(): string
42
    {
43 36
        return $this->product->getId();
44
    }
45
    
46 1
    public function getName(): string
47
    {
48 1
        return $this->product->getName();
49
    }
50
51 1
    public function getImagePath(): string
52
    {
53 1
        return $this->product->getImagePath();
54
    }
55
    
56 10
    public function getQuantity(): int
57
    {
58 10
        return $this->quantity;
59
    }
60
    
61 42
    public function setQuantity(int $quantity): void
62
    {
63 42
        if (!$this->validateQuantity($quantity)) {
64 5
            throw new QuanityException('Invalid quantity: ' . $quantity);
65
        }
66
        
67 41
        $this->quantity = $quantity;
68 41
    }
69
    
70 2
    public function increaseQuantity(int $quantity): void
71
    {
72 2
        $newQuantity = $this->quantity + $quantity;
73
        
74 2
        $this->setQuantity($newQuantity);
75 2
    }
76
    
77 2
    public function decreaseQuantity(int $quantity): void
78
    {
79 2
        $newQuantity = $this->quantity - $quantity;
80
        
81 2
        $this->setQuantity($newQuantity);
82 1
    }
83
84 3
    public function getWeight(): float
85
    {
86 3
        return $this->product->getWeight();
87
    }
88
    
89 3
    public function getWeightTotal(): float
90
    {
91 3
        return (float) bcmul((string) $this->getWeight(), (string) $this->quantity, 2);
92
    }
93
94 9
    public function getTax(string $currency): Money
95
    {
96 9
        return $this->product->getTax($currency);
97
    }
98
            
99 9
    public function getTaxTotal(string $currency): Money
100
    {
101 9
        return $this->getTax($currency)->multiply($this->quantity);
102
    }
103
    
104 9
    public function getPrice(string $currency): Money
105
    {
106 9
        return $this->product->getPrice($currency);
107
    }
108
    
109 9
    public function getPriceTotal(string $currency): Money
110
    {
111 9
        return $this->getPrice($currency)->multiply($this->quantity);
112
    }
113
    
114 1
    public function getPriceWithTax(string $currency): Money
115
    {
116 1
        return $this->product->getPriceWithTax($currency);
117
    }
118
    
119 8
    public function getPriceTotalWithTax(string $currency): Money
120
    {
121 8
        return $this->getPriceTotal($currency)->add($this->getTaxTotal($currency));
122
    }
123
    
124 2
    public function toArray(string $currency): array
125
    {
126 2
        $array = [];
127 2
        $array['quantity']          = $this->getQuantity();
128 2
        $array['totalTax']          = $this->getTaxTotal($currency);
129 2
        $array['priceTotal']        = $this->getPriceTotal($currency);
130 2
        $array['priceTotalWithTax'] = $this->getPriceTotalWithTax($currency);
131 2
        $array['product']           = $this->product->toArray($currency);
132
        
133 2
        return $array;
134
    }
135
    
136 42
    private function validateQuantity(int $quantity): bool
137
    {
138 42
        if (is_null($this->quantityValidator)) {
139 39
            if ($quantity < 1) {
140 1
                return false;
141
            }
142
143 39
            return true;
144
        }
145
        
146 5
        return $this->quantityValidator->validate($this->product, $quantity);
147
    }
148
}
149