DoctrineCartRepository::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Simara\Cart\Infrastructure;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\NoResultException;
7
use Simara\Cart\Domain\Cart\Cart;
8
use Simara\Cart\Domain\Cart\CartNotFoundException;
9
use Simara\Cart\Domain\Cart\CartRepository;
10
use TypeError;
11
12
use function assert;
13
14
final class DoctrineCartRepository implements CartRepository
15
{
16 10
    public function __construct(private EntityManager $entityManger)
17
    {
18 10
    }
19 10
20
    public function add(Cart $cart): void
21 7
    {
22
        $this->entityManger->persist($cart);
23 7
    }
24 7
25
    public function get(string $id): Cart
26 9
    {
27
        $queryBuilder = $this->entityManger->createQueryBuilder();
28 9
        $queryBuilder
29
            ->select('cart, items')
30 9
            ->from(Cart::class, 'cart')
31 9
            ->leftJoin('cart.items', 'items')
32 9
            ->where('cart.id = :id')
33 9
            ->setParameter(':id', $id);
34 9
35
        $query = $queryBuilder->getQuery();
36 9
37
        try {
38
            $cart = $query->getSingleResult();
39 9
            assert($cart instanceof Cart);
40 7
            return $cart;
41 7
        } catch (NoResultException) {
42 3
            throw new CartNotFoundException();
43 3
        }
44
    }
45
46
    public function remove(string $id): void
47 3
    {
48
        $cart = $this->get($id);
49 3
        $this->entityManger->remove($cart);
50 2
    }
51
}
52