Completed
Pull Request — master (#16)
by Josef
05:49
created

Cart::getCurrentPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway;
4
5
class Cart
6
{
7
8
	/**
9
	 * @var CartItem[]
10
	 */
11
	private $items = [];
12
13
	/**
14
	 * @var Currency
15
	 */
16
	private $currency;
17
18
	public function __construct(Currency $currency)
19
	{
20
		$this->currency = $currency;
21
	}
22
23
	public function addItem(string $name, int $quantity, int $amount, string $description = null)
24
	{
25
		$this->items[] = new CartItem($name, $quantity, $amount, $description);
26
	}
27
28
	/**
29
	 * @return CartItem[]
30
	 */
31
	public function getItems(): array
32
	{
33
		return $this->items;
34
	}
35
36
	public function getCurrentPrice(): Price
37
	{
38
		return new Price(
39
			$this->countTotalAmount(),
40
			$this->currency
41
		);
42
	}
43
44
	private function countTotalAmount(): int
45
	{
46
		$totalAmount = 0;
47
48
		foreach ($this->items as $item) {
49
			$totalAmount += $item->getAmount();
50
		}
51
52
		return $totalAmount;
53
	}
54
55
}
56