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

EnabledSecondFactorRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getAllMatchingFor() 0 21 3
A findOneBySecondFactorId() 0 10 3
A getInstitutionByNameId() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
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 Psr\Log\LoggerInterface;
23
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
24
use Surfnet\StepupBundle\Value\Loa;
25
26
final class EnabledSecondFactorRepository implements SecondFactorRepository
27
{
28
    /**
29
     * @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactorRepository
30
     */
31
    private $secondFactorRepository;
32
33
    /**
34
     * @var string[]
35
     */
36
    private $enabledTypes;
37
38
    /**
39
     * @var LoggerInterface
40
     */
41
    private $logger;
42
43
    /**
44
     * @param SecondFactorRepository $secondFactorRepository
45
     * @param string[]               $enabledTypes
46
     * @param LoggerInterface        $logger
47
     */
48
    public function __construct(
49
        SecondFactorRepository $secondFactorRepository,
50
        array $enabledTypes,
51
        LoggerInterface $logger
52
    ) {
53
        $this->secondFactorRepository = $secondFactorRepository;
54
        $this->enabledTypes = array_combine($enabledTypes, $enabledTypes);
55
        $this->logger = $logger;
56
    }
57
58
    public function getAllMatchingFor(Loa $highestLoa, $identityNameId, SecondFactorTypeService $service)
59
    {
60
        $enabledSecondFactors = new ArrayCollection();
61
62
        foreach ($this->secondFactorRepository->getAllMatchingFor($highestLoa, $identityNameId, $service) as $secondFactor) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
63
            if (!array_key_exists($secondFactor->secondFactorType, $this->enabledTypes)) {
64
                $this->logger->info(
65
                    sprintf(
66
                        'Discarding second factor; its second factor type "%s" is not enabled',
67
                        $secondFactor->secondFactorType
68
                    )
69
                );
70
71
                continue;
72
            }
73
74
            $enabledSecondFactors->add($secondFactor);
75
        }
76
77
        return $enabledSecondFactors;
78
    }
79
80
    public function findOneBySecondFactorId($secondFactorId)
81
    {
82
        $secondFactor = $this->secondFactorRepository->findOneBySecondFactorId($secondFactorId);
83
84
        if (!$secondFactor || !array_key_exists($secondFactor->secondFactorType, $this->enabledTypes)) {
85
            return null;
86
        }
87
88
        return $secondFactor;
89
    }
90
91
    public function getInstitutionByNameId($identityNameId)
92
    {
93
        return $this->secondFactorRepository->getInstitutionByNameId($identityNameId);
94
    }
95
}
96