Completed
Push — feature/vetting-location ( 0a3f42...e920c1 )
by A.
04:06
created

RaLocationRepository::search()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 18
rs 9.4285
cc 3
eloc 13
nc 3
nop 1
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
            ->getArrayResult();
97
    }
98
}
99