Passed
Push — master ( caf235...1c2ae4 )
by Svaťa
03:23
created

DoctrineCartRepository::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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