Test Failed
Pull Request — master (#1)
by Svaťa
05:09
created

DoctrineCartRepository::getThrowingException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Simara\Cart\Infrastructure;
4
5
use Doctrine\ORM\EntityManager;
6
use Simara\Cart\Domain\Cart;
7
use Simara\Cart\Domain\CartNotFoundException;
8
use Simara\Cart\Domain\CartRepository;
9
use TypeError;
10
11
class DoctrineCartRepository implements CartRepository
12
{
13
14
    /**
15
     * @var EntityManager
16
     */
17
    private $entityManger;
18
19 9
    public function __construct(EntityManager $entityManger)
20
    {
21 9
        $this->entityManger = $entityManger;
22 9
    }
23
24 7
    public function add(Cart $cart): void
25
    {
26 7
        $this->entityManger->persist($cart);
27 7
    }
28
29 9
    public function get(string $id): Cart
30
    {
31 9
        $result = $this->find($id);
32
        if ($result === null) {
33
            throw new CartNotFoundException();
34 7
        }
35
        return $result;
36 7
    }
37
38
    private function find(string $id): ?Cart
39 9
    {
40
        $queryBuilder = $this->entityManger->createQueryBuilder();
41
        $queryBuilder
42 9
            ->select('cart, items')
43 3
            ->from(Cart::class, 'cart')
44 3
            ->leftJoin('cart.items', 'items')
45
            ->where('cart.id = :id')
46
            ->setParameter(':id', $id);
47
48 3
        $query = $queryBuilder->getQuery();
49
        return $query->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getResult() could return the type array which is incompatible with the type-hinted return null|Simara\Cart\Domain\Cart. Consider adding an additional type-check to rule them out.
Loading history...
50 3
    }
51 2
52 2
    public function remove(string $id): void
53
    {
54
        $cart = $this->get($id);
55
        $this->entityManger->remove($cart);
56
    }
57
}
58