Test Failed
Pull Request — master (#3)
by Svaťa
03:32
created

CartUseCase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Simara\Cart\Application;
5
6
use Simara\Cart\Domain\Cart;
7
use Simara\Cart\Domain\CartDetail;
8
use Simara\Cart\Domain\CartNotFoundException;
9
use Simara\Cart\Domain\CartRepository;
10
use Simara\Cart\Domain\Prices;
11
12
final class CartUseCase
13
{
14
	/**
15
	 * @var CartRepository
16
	 */
17
	private $repository;
18
19
	/**
20
	 * @var Prices
21
	 */
22
	private $prices;
23
24
	public function __construct(CartRepository $repository, Prices $prices)
25
	{
26
		$this->repository = $repository;
27
		$this->prices = $prices;
28
	}
29
30
	public function add(string $cartId, string $productId, int $amount): void
31
	{
32
		$cart = $this->get($cartId);
33
		$cart->add($productId, $amount);
34
		$this->repository->add($cart);
35
	}
36
37
	public function detail(string $cartId): CartDetail
38
	{
39
		return $this->get($cartId)->calculate($this->prices);
40
	}
41
42
	private function get(string $cartId): Cart
43
	{
44
		try {
45
			return $this->repository->get($cartId);
46
		} catch (CartNotFoundException $e) {
47
			return new Cart($cartId);
48
		}
49
	}
50
51
}
52