Completed
Push — feature/invert-ra-location-con... ( 54bafc )
by A.
10:46 queued 06:44
created

InstitutionWithRaLocationsRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addIfNotExists() 0 16 2
A institutionShowsRaLocations() 0 10 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\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionWithRaLocations;
24
25
final class InstitutionWithRaLocationsRepository extends EntityRepository
26
{
27
    /**
28
     * @param InstitutionWithRaLocations $institutionWithRaLocations
29
     */
30
    public function addIfNotExists(InstitutionWithRaLocations $institutionWithRaLocations)
31
    {
32
        $existsQuery = $this->createQueryBuilder('ipr')
33
            ->where('ipr.institution = :institution')
34
            ->setParameter('institution', $institutionWithRaLocations->institution)
35
            ->getQuery()
36
            ->getOneOrNullResult();
37
38
        if ($existsQuery) {
39
            return;
40
        }
41
42
        $em = $this->getEntityManager();
43
        $em->persist($institutionWithRaLocations);
44
        $em->flush($institutionWithRaLocations);
45
    }
46
47
    /**
48
     * @param Institution $institution
49
     * @return boolean
50
     */
51
    public function institutionShowsRaLocations(Institution $institution)
52
    {
53
        $matchedInstitutions = $this->createQueryBuilder('irl')
54
            ->where('irl.institution = :institution')
55
            ->setParameter('institution', $institution->getInstitution())
56
            ->getQuery()
57
            ->getArrayResult();
58
59
        return !empty($matchedInstitutions);
60
    }
61
}
62