Completed
Push — master ( 2d9e7d...e8a8b9 )
by Samuel
8s
created

VehicleRepository::findAll()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace App\Vehicle\Infra\Repository;
4
5
use App\Common\Infra\Repository\DataRepository;
6
use App\Vehicle\App\Query\VehiclesQuery;
7
use App\Person\Domain\Person;
8
use App\Vehicle\Domain\VehicleAbstract;
9
use App\Vehicle\Domain\VehicleInterface;
10
use App\Vehicle\Domain\VehicleRepositoryInterface;
11
use Doctrine\Common\Persistence\ObjectManager;
12
use Doctrine\ORM\EntityRepository;
13
14
class VehicleRepository implements VehicleRepositoryInterface
15
{
16
    /**
17
     * @var ObjectManager
18
     */
19
    private $em;
20
21
    /**
22
     * @param ObjectManager $em
23
     */
24
    public function __construct(ObjectManager $em)
25
    {
26
        $this->em = $em;
27
    }
28
29
    /**
30
     * @param Person $person
31
     * @return array
32
     */
33
    public function findByPerson(Person $person): array
34
    {
35
        $qb = $this->getRepository()->createQueryBuilder('v');
36
37
        $qb
38
            ->innerJoin(Person::class, 'p')
39
            ->innerJoin('p.vehicles', 'phv', \Doctrine\ORM\Query\Expr\Join::WITH, 'phv.id = v.id')
40
            ->andWhere(
41
                $qb->expr()->eq(
42
                    'p.id',
43
                    $qb->expr()->literal($person->getId())
44
                )
45
            );
46
47
        return $qb->getQuery()->getResult();
48
    }
49
50
    /**
51
     * @param VehiclesQuery $query
52
     * @return array
53
     */
54
    public function findAll(VehiclesQuery $query): array
55
    {
56
        $qb = $this->getRepository()->createQueryBuilder('v');
57
58
        if ($query->hasPersonId()) {
59
            $qb
60
                ->innerJoin(Person::class, 'p')
61
                ->innerJoin('p.vehicles', 'phv', \Doctrine\ORM\Query\Expr\Join::WITH, 'phv.id = v.id')
62
                ->andWhere(
63
                    $qb->expr()->eq(
64
                        'p.id',
65
                        $qb->expr()->literal($query->getPersonId())
66
                    )
67
                )
68
            ;
69
        }
70
71
        if ($query->hasVehicleId()) {
72
            $qb->andWhere(
73
                $qb->expr()->eq(
74
                    'v.id',
75
                    $qb->expr()->literal($query->getVehicleId())
76
                )
77
            );
78
        }
79
80
        return $qb->getQuery()->getResult();
81
    }
82
83
    /**
84
     * @param string $id
85
     * @return Car|null
86
     * @throws \Doctrine\ORM\NonUniqueResultException
87
     */
88
    public function find(string $id): ?VehicleInterface
89
    {
90
        $qb = $this->getRepository()->createQueryBuilder('v');
91
92
        $qb->andWhere(
93
            $qb->expr()->eq(
94
                'v.id',
95
                $qb->expr()->literal($id)
96
            )
97
        );
98
99
        return $qb->getQuery()->getOneOrNullResult();
100
    }
101
102
    /**
103
     * @param VehicleInterface $vehicle
104
     * @return VehicleInterface
105
     */
106
    public function save(VehicleInterface $vehicle): VehicleInterface
107
    {
108
        $this->em->persist($vehicle);
109
        $this->em->flush();
110
111
        return $vehicle;
112
    }
113
114
    /**
115
     * @param VehicleInterface $vehicle
116
     */
117
    public function delete(VehicleInterface $vehicle): void
118
    {
119
        $this->em->remove($vehicle);
120
        $this->em->flush();
121
    }
122
123
    /**
124
     * @return EntityRepository
125
     */
126
    protected function getRepository(): EntityRepository
127
    {
128
        return $this->em->getRepository(VehicleAbstract::class);
129
    }
130
}
131