Completed
Pull Request — develop (#123)
by A.
09:21 queued 04:30
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\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation;
23
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Query\RaLocationQuery;
24
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
25
26
class RaLocationRepository extends EntityRepository
27
{
28
    /**
29
     * @param RaLocationQuery $query
30
     * @return null|RaLocation[]
31
     */
32
    public function search(RaLocationQuery $query)
33
    {
34
        if (!in_array($query->orderBy, ['name', 'location', 'contact_information'])) {
35
            throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
36
        }
37
38
        $orderBy        = 'rl.'.$query->orderBy;
39
        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';
40
41
        return $this->getEntityManager()->createQueryBuilder()
42
            ->select('rl')
43
            ->from('Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation', 'rl')
44
            ->where('rl.institution = :institution')
45
            ->setParameter('institution', $query->institution->getInstitution())
46
            ->orderBy($orderBy, $orderDirection)
47
            ->getQuery()
48
            ->getResult();
49
    }
50
51
    /**
52
     * @param RaLocation $raLocation
53
     */
54
    public function save(RaLocation $raLocation)
55
    {
56
        $entityManager = $this->getEntityManager();
57
        $entityManager->persist($raLocation);
58
        $entityManager->flush();
59
    }
60
}
61