Passed
Push — feature/add-azure-mfa-registra... ( e9464b...2712a3 )
by
unknown
02:12
created

DoctrineSecondFactorRepository::hasTokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Copyright 2015 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 hasTokens(string $identityNameId): bool
49
    {
50
        $secondFactors = $this->findAllByIdentityNameId($identityNameId);
51
        return count($secondFactors) > 0;
52
    }
53
54
    public function findOneBySecondFactorId($secondFactorId)
55
    {
56
        if (!isset($this->secondFactorsById[$secondFactorId])) {
57
            $this->secondFactorsById[$secondFactorId] = $this->findOneBy(['secondFactorId' => $secondFactorId]);
58
        }
59
60
        return $this->secondFactorsById[$secondFactorId];
61
    }
62
63
    /**
64
     * @param $identityNameId
65
     * @return string
66
     */
67
    public function getInstitutionByNameId($identityNameId)
68
    {
69
        /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor $secondFactor **/
70
        $secondFactor = $this->createQueryBuilder('sf')
71
            ->where('sf.nameId = :nameId')
72
            ->setParameter('nameId', $identityNameId)
73
            ->setMaxResults(1)
74
            ->getQuery()
75
            ->getOneOrNullResult();
76
77
        if ($secondFactor) {
0 ignored issues
show
introduced by
$secondFactor is of type Surfnet\StepupGateway\Ga...dle\Entity\SecondFactor, thus it always evaluated to true.
Loading history...
78
            return $secondFactor->institution;
79
        }
80
        return '';
81
    }
82
83
    /**
84
     * @param $identityNameId
85
     * @return \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[]
86
     */
87
    private function findAllByIdentityNameId($identityNameId)
88
    {
89
        /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[] $secondFactors */
90
        return $this->createQueryBuilder('sf')
91
            ->where('sf.nameId = :nameId')
92
            ->setParameter('nameId', $identityNameId)
93
            ->getQuery()
94
            ->getResult();
95
    }
96
}
97