-
Notifications
You must be signed in to change notification settings - Fork 11
/
DoctrineCartRepository.php
51 lines (42 loc) · 1.25 KB
/
DoctrineCartRepository.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
42
43
44
45
46
47
48
49
50
51
<?php
namespace Simara\Cart\Infrastructure;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\NoResultException;
use Simara\Cart\Domain\Cart\Cart;
use Simara\Cart\Domain\Cart\CartNotFoundException;
use Simara\Cart\Domain\Cart\CartRepository;
use TypeError;
use function assert;
final class DoctrineCartRepository implements CartRepository
{
public function __construct(private EntityManager $entityManger)
{
}
public function add(Cart $cart): void
{
$this->entityManger->persist($cart);
}
public function get(string $id): Cart
{
$queryBuilder = $this->entityManger->createQueryBuilder();
$queryBuilder
->select('cart, items')
->from(Cart::class, 'cart')
->leftJoin('cart.items', 'items')
->where('cart.id = :id')
->setParameter(':id', $id);
$query = $queryBuilder->getQuery();
try {
$cart = $query->getSingleResult();
assert($cart instanceof Cart);
return $cart;
} catch (NoResultException) {
throw new CartNotFoundException();
}
}
public function remove(string $id): void
{
$cart = $this->get($id);
$this->entityManger->remove($cart);
}
}