Completed
Push — feature/refactor-loa-determina... ( 42efb1 )
by
unknown
28:09
created

DoctrineSecondFactorRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllMatchingFor() 0 14 3
A findOneBySecondFactorId() 0 8 2
A getInstitutionByNameId() 0 15 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\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);
0 ignored issues
show
Documentation Bug introduced by
The method findAllByIdentityNameId does not exist on object<Surfnet\StepupGat...SecondFactorRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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