Completed
Push — feature/refactor-loa-determina... ( 0692c2...b4d0d3 )
by
unknown
05:19
created

getInstitutionByNameId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\StepupGateway\GatewayBundle\Entity;
20
21
use Doctrine\Common\Collections\ArrayCollection;
22
use Doctrine\ORM\EntityRepository;
23
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
24
use Surfnet\StepupBundle\Value\Loa;
25
26
class DoctrineSecondFactorRepository extends EntityRepository implements SecondFactorRepository
27
{
28
    /**
29
     * @var SecondFactor[]
30
     */
31
    private $secondFactorsById = [];
32
33
    public function getAllMatchingFor(Loa $highestLoa, $identityNameId, SecondFactorTypeService $service)
34
    {
35
        /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[] $secondFactors */
36
        $secondFactors = $this->findAllByIdentityNameId($identityNameId);
37
38
        $matches = new ArrayCollection();
39
        foreach ($secondFactors as $secondFactor) {
40
            if ($secondFactor->canSatisfy($highestLoa, $service)) {
41
                $matches->add($secondFactor);
42
            }
43
        }
44
45
        return $matches;
46
    }
47
48
    public function findOneBySecondFactorId($secondFactorId)
49
    {
50
        if (!isset($this->secondFactorsById[$secondFactorId])) {
51
            $this->secondFactorsById[$secondFactorId] = $this->findOneBy(['secondFactorId' => $secondFactorId]);
52
        }
53
54
        return $this->secondFactorsById[$secondFactorId];
55
    }
56
57
    /**
58
     * @param $identityNameId
59
     * @return string
60
     */
61
    public function getInstitutionByNameId($identityNameId)
62
    {
63
        /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor $secondFactor **/
64
        $secondFactor = $this->createQueryBuilder('sf')
65
            ->where('sf.nameId = :nameId')
66
            ->setParameter('nameId', $identityNameId)
67
            ->setMaxResults(1)
68
            ->getQuery()
69
            ->getOneOrNullResult();
70
71
        if ($secondFactor) {
72
            return $secondFactor->institution;
73
        }
74
        return '';
75
    }
76
77
    /**
78
     * @param $identityNameId
79
     * @return \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[]
80
     */
81
    private function findAllByIdentityNameId($identityNameId)
82
    {
83
        /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[] $secondFactors */
84
        return $this->createQueryBuilder('sf')
85
            ->where('sf.nameId = :nameId')
86
            ->setParameter('nameId', $identityNameId)
87
            ->getQuery()
88
            ->getResult();
89
    }
90
}
91