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

DoctrineCartRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 45
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

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