Passed
Push — master ( 9316b9...0b6d0a )
by Svaťa
02:41
created

CartUseCase::detail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 2
	public function __construct(CartRepository $repository, Prices $prices)
25
	{
26 2
		$this->repository = $repository;
27 2
		$this->prices = $prices;
28 2
	}
29
30 1
	public function add(string $cartId, string $productId, int $amount): void
31
	{
32 1
		$cart = $this->get($cartId);
33 1
		$cart->add($productId, $amount);
34 1
		$this->repository->add($cart);
35 1
	}
36
37 1
	public function detail(string $cartId): CartDetail
38
	{
39 1
		return $this->get($cartId)->calculate($this->prices);
40
	}
41
42 1
	private function get(string $cartId): Cart
43
	{
44
		try {
45 1
			return $this->repository->get($cartId);
46 1
		} catch (CartNotFoundException $e) {
47 1
			return new Cart($cartId);
48
		}
49
	}
50
51
}
52