|
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
|
|
|
if ($query->hasAfter()) { |
|
81
|
|
|
$qb->andWhere( |
|
82
|
|
|
$qb->expr()->gt( |
|
83
|
|
|
'v.id', |
|
84
|
|
|
$qb->expr()->literal($query->getAfter()) |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($query->hasOffset()) { |
|
90
|
|
|
$qb->setFirstResult($query->getOffset()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($query->hasLimit()) { |
|
94
|
|
|
$qb->setMaxResults($query->getLimit()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$qb->orderBy('v.id', 'ASC'); |
|
98
|
|
|
|
|
99
|
|
|
return $qb->getQuery()->getResult(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $id |
|
104
|
|
|
* @return Car|null |
|
105
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function find(string $id): ?VehicleInterface |
|
108
|
|
|
{ |
|
109
|
|
|
$qb = $this->getRepository()->createQueryBuilder('v'); |
|
110
|
|
|
|
|
111
|
|
|
$qb->andWhere( |
|
112
|
|
|
$qb->expr()->eq( |
|
113
|
|
|
'v.id', |
|
114
|
|
|
$qb->expr()->literal($id) |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
return $qb->getQuery()->getOneOrNullResult(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param VehicleInterface $vehicle |
|
123
|
|
|
* @return VehicleInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
public function save(VehicleInterface $vehicle): VehicleInterface |
|
126
|
|
|
{ |
|
127
|
|
|
$this->em->persist($vehicle); |
|
128
|
|
|
$this->em->flush(); |
|
129
|
|
|
|
|
130
|
|
|
return $vehicle; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param VehicleInterface $vehicle |
|
135
|
|
|
*/ |
|
136
|
|
|
public function delete(VehicleInterface $vehicle): void |
|
137
|
|
|
{ |
|
138
|
|
|
$this->em->remove($vehicle); |
|
139
|
|
|
$this->em->flush(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return EntityRepository |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function getRepository(): EntityRepository |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->em->getRepository(VehicleAbstract::class); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|