Completed
Push — bugfix/consider-sp-specific-lo... ( a05dff )
by
unknown
16:53
created

DoctrineSecondFactorRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 4
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllMatchingFor() 0 14 3
A findOneBySecondFactorId() 0 8 2
A getAllInstitutions() 0 10 2
A findAllByIdentityNameId() 0 9 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
     * Loads the institutions for a given identity name id
59
     *
60
     * @param $identityNameId
61
     * @return array
62
     */
63
    public function getAllInstitutions($identityNameId)
64
    {
65
        $institutions = [];
66
        $secondFactors = $this->findAllByIdentityNameId($identityNameId);
67
        foreach ($secondFactors as $secondFactor) {
68
            $institutions[$secondFactor->institution] = $secondFactor->institution;
69
        }
70
71
        return $institutions;
72
    }
73
74
    /**
75
     * @param $identityNameId
76
     * @return \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[]
77
     */
78
    private function findAllByIdentityNameId($identityNameId)
79
    {
80
        /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[] $secondFactors */
81
        return $this->createQueryBuilder('sf')
82
            ->where('sf.nameId = :nameId')
83
            ->setParameter('nameId', $identityNameId)
84
            ->getQuery()
85
            ->getResult();
86
    }
87
}
88