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\RaLocationId; |
23
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Query\RaLocationQuery; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException; |
26
|
|
|
|
27
|
|
|
class RaLocationRepository extends EntityRepository |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param RaLocationQuery $query |
31
|
|
|
* @return null|RaLocation[] |
32
|
|
|
*/ |
33
|
|
|
public function search(RaLocationQuery $query) |
34
|
|
|
{ |
35
|
|
|
if (!in_array($query->orderBy, ['name', 'location', 'contact_information'])) { |
36
|
|
|
throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$orderBy = 'rl.'.$query->orderBy; |
40
|
|
|
$orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC'; |
41
|
|
|
|
42
|
|
|
return $this->getEntityManager()->createQueryBuilder() |
43
|
|
|
->select('rl') |
44
|
|
|
->from('Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation', 'rl') |
45
|
|
|
->where('rl.institution = :institution') |
46
|
|
|
->setParameter('institution', $query->institution->getInstitution()) |
47
|
|
|
->orderBy($orderBy, $orderDirection) |
48
|
|
|
->getQuery() |
49
|
|
|
->getResult(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function findByRaLocationId(RaLocationId $raLocationId) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
return $this->createQueryBuilder('rl') |
55
|
|
|
->where('rl.id = :id') |
56
|
|
|
->setParameter('id', $raLocationId->getRaLocationId()) |
57
|
|
|
->getQuery() |
58
|
|
|
->getOneOrNullResult(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param RaLocation $raLocation |
63
|
|
|
*/ |
64
|
|
|
public function save(RaLocation $raLocation) |
65
|
|
|
{ |
66
|
|
|
$entityManager = $this->getEntityManager(); |
67
|
|
|
$entityManager->persist($raLocation); |
68
|
|
|
$entityManager->flush(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param RaLocation $raLocation |
73
|
|
|
*/ |
74
|
|
|
public function remove(RaLocation $raLocation) |
75
|
|
|
{ |
76
|
|
|
$entityManager = $this->getEntityManager(); |
77
|
|
|
$entityManager->remove($raLocation); |
78
|
|
|
$entityManager->flush(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.