|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2016 SURFnet B.V. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository; |
|
20
|
|
|
|
|
21
|
|
|
use Doctrine\ORM\EntityRepository; |
|
22
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
|
23
|
|
|
use Surfnet\Stepup\Configuration\Value\RaLocationId; |
|
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation; |
|
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Query\RaLocationQuery; |
|
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException; |
|
27
|
|
|
|
|
28
|
|
|
class RaLocationRepository extends EntityRepository |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @param RaLocationQuery $query |
|
32
|
|
|
* @return null|RaLocation[] |
|
33
|
|
|
*/ |
|
34
|
|
|
public function search(RaLocationQuery $query) |
|
35
|
|
|
{ |
|
36
|
|
|
if (!in_array($query->orderBy, ['name', 'location', 'contact_information'])) { |
|
37
|
|
|
throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$orderBy = 'rl.'.$query->orderBy; |
|
41
|
|
|
$orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC'; |
|
42
|
|
|
|
|
43
|
|
|
return $this->getEntityManager()->createQueryBuilder() |
|
44
|
|
|
->select('rl') |
|
45
|
|
|
->from('Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation', 'rl') |
|
46
|
|
|
->where('rl.institution = :institution') |
|
47
|
|
|
->setParameter('institution', $query->institution->getInstitution()) |
|
48
|
|
|
->orderBy($orderBy, $orderDirection) |
|
49
|
|
|
->getQuery() |
|
50
|
|
|
->getResult(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param RaLocationId $raLocationId |
|
55
|
|
|
* @return RaLocation[] |
|
56
|
|
|
*/ |
|
57
|
|
|
public function findByRaLocationId(RaLocationId $raLocationId) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->createQueryBuilder('rl') |
|
60
|
|
|
->where('rl.id = :id') |
|
61
|
|
|
->setParameter('id', $raLocationId->getRaLocationId()) |
|
62
|
|
|
->getQuery() |
|
63
|
|
|
->getOneOrNullResult(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param RaLocation $raLocation |
|
68
|
|
|
*/ |
|
69
|
|
|
public function save(RaLocation $raLocation) |
|
70
|
|
|
{ |
|
71
|
|
|
$entityManager = $this->getEntityManager(); |
|
72
|
|
|
$entityManager->persist($raLocation); |
|
73
|
|
|
$entityManager->flush(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param RaLocation $raLocation |
|
78
|
|
|
*/ |
|
79
|
|
|
public function remove(RaLocation $raLocation) |
|
80
|
|
|
{ |
|
81
|
|
|
$entityManager = $this->getEntityManager(); |
|
82
|
|
|
$entityManager->remove($raLocation); |
|
83
|
|
|
$entityManager->flush(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Institution $institution |
|
88
|
|
|
* @return RaLocation[] |
|
89
|
|
|
*/ |
|
90
|
|
|
public function findByInstitution(Institution $institution) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->createQueryBuilder('rl') |
|
93
|
|
|
->where('rl.institution = :institution') |
|
94
|
|
|
->setParameter('institution', $institution->getInstitution()) |
|
95
|
|
|
->getQuery() |
|
96
|
|
|
->getResult(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Institution $institution |
|
101
|
|
|
*/ |
|
102
|
|
|
public function removeRaLocationsFor(Institution $institution) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->createQueryBuilder('rl') |
|
105
|
|
|
->delete() |
|
106
|
|
|
->where('rl.institution = :institution') |
|
107
|
|
|
->setParameter('institution', $institution->getInstitution()) |
|
108
|
|
|
->getQuery() |
|
109
|
|
|
->execute(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|