DoctrineCartRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 36
ccs 21
cts 21
cp 1
rs 10
c 2
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 18 2
A add() 0 3 1
A __construct() 0 2 1
A remove() 0 4 1
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