Completed
Pull Request — master (#102)
by Boy
18:16
created

DoctrineSecondFactorRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllMatchingFor() 0 18 3
A findOneBySecondFactorId() 0 8 2
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\Value\Loa;
24
25
class DoctrineSecondFactorRepository extends EntityRepository implements SecondFactorRepository
26
{
27
    /**
28
     * @var SecondFactor[]
29
     */
30
    private $secondFactorsById = [];
31
32
    public function getAllMatchingFor(Loa $highestLoa, $identityNameId)
33
    {
34
        /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor[] $secondFactors */
35
        $secondFactors = $this->createQueryBuilder('sf')
36
            ->where('sf.nameId = :nameId')
37
            ->setParameter('nameId', $identityNameId)
38
            ->getQuery()
39
            ->getResult();
40
41
        $matches = new ArrayCollection();
42
        foreach ($secondFactors as $secondFactor) {
43
            if ($secondFactor->canSatisfy($highestLoa)) {
44
                $matches->add($secondFactor);
45
            }
46
        }
47
48
        return $matches;
49
    }
50
51
    public function findOneBySecondFactorId($secondFactorId)
52
    {
53
        if (!isset($this->secondFactorsById[$secondFactorId])) {
54
            $this->secondFactorsById[$secondFactorId] = $this->findOneBy(['secondFactorId' => $secondFactorId]);
55
        }
56
57
        return $this->secondFactorsById[$secondFactorId];
58
    }
59
}
60