-
Notifications
You must be signed in to change notification settings - Fork 11
/
CartUseCaseApplication.php
41 lines (34 loc) · 1018 Bytes
/
CartUseCaseApplication.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
declare(strict_types=1);
namespace Simara\Cart\Application;
use Simara\Cart\Domain\Cart\Cart;
use Simara\Cart\Domain\Cart\CartDetail;
use Simara\Cart\Domain\Cart\CartNotFoundException;
use Simara\Cart\Domain\Cart\CartRepository;
use Simara\Cart\Domain\Prices\Prices;
final class CartUseCaseApplication implements CartUseCase
{
public function __construct(
private CartRepository $repository,
private Prices $prices
) {
}
public function add(string $cartId, string $productId, int $amount): void
{
$cart = $this->get($cartId);
$cart->add($productId, $amount);
$this->repository->add($cart);
}
public function detail(string $cartId): CartDetail
{
return $this->get($cartId)->calculate($this->prices);
}
private function get(string $cartId): Cart
{
try {
return $this->repository->get($cartId);
} catch (CartNotFoundException $e) {
return new Cart($cartId);
}
}
}