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

Cart   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 51
rs 10
c 2
b 1
f 0
wmc 6
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addItem() 0 4 1
A getItems() 0 4 1
A countTotalAmount() 0 10 2
A getCurrentPrice() 0 7 1
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