CartUseCaseApplication::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
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 CartUseCaseApplication implements CartUseCase
14
{
15
    public function __construct(
16
        private CartRepository $repository,
17
        private Prices $prices
18
    ) {
19
    }
20
21
    public function add(string $cartId, string $productId, int $amount): void
22
    {
23
        $cart = $this->get($cartId);
24
        $cart->add($productId, $amount);
25
        $this->repository->add($cart);
26
    }
27
28
    public function detail(string $cartId): CartDetail
29
    {
30
        return $this->get($cartId)->calculate($this->prices);
31
    }
32
33
    private function get(string $cartId): Cart
34
    {
35
        try {
36
            return $this->repository->get($cartId);
37
        } catch (CartNotFoundException $e) {
38
            return new Cart($cartId);
39
        }
40
    }
41
}
42