Passed
Pull Request — master (#11)
by Svaťa
02:58
created

CartUseCase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 30
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Simara\Cart\Application;
6
7
use Simara\Cart\Domain\Cart\Cart;
8
use Simara\Cart\Domain\Cart\CartDetail;
9
use Simara\Cart\Domain\Cart\CartNotFoundException;
10
use Simara\Cart\Domain\Cart\CartRepository;
11
use Simara\Cart\Domain\Prices\Prices;
12
13
final class CartUseCase
14
{
15
    public function __construct(
16
        private CartRepository $repository,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_PRIVATE, expecting T_VARIABLE on line 16 at column 8
Loading history...
17
        private Prices $prices
18
    ) {
19 2
    }
20
21 2
    public function add(string $cartId, string $productId, int $amount): void
22 2
    {
23 2
        $cart = $this->get($cartId);
24
        $cart->add($productId, $amount);
25 1
        $this->repository->add($cart);
26
    }
27 1
28 1
    public function detail(string $cartId): CartDetail
29 1
    {
30 1
        return $this->get($cartId)->calculate($this->prices);
31
    }
32 1
33
    private function get(string $cartId): Cart
34 1
    {
35
        try {
36
            return $this->repository->get($cartId);
37 1
        } catch (CartNotFoundException $e) {
38
            return new Cart($cartId);
39
        }
40 1
    }
41
}
42