Completed
Push — release-1.x ( 0efa6c...32f2bb )
by Boy
07:06 queued 03:32
created

EnabledSecondFactorRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getAllMatchingFor() 0 21 3
A findOneBySecondFactorId() 0 10 3
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\Value\Loa;
24
25
final class EnabledSecondFactorRepository implements SecondFactorRepository
26
{
27
    /**
28
     * @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactorRepository
29
     */
30
    private $secondFactorRepository;
31
32
    /**
33
     * @var string[]
34
     */
35
    private $enabledTypes;
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $logger;
40
41
    /**
42
     * @param SecondFactorRepository $secondFactorRepository
43
     * @param string[]               $enabledTypes
44
     * @param LoggerInterface        $logger
45
     */
46
    public function __construct(
47
        SecondFactorRepository $secondFactorRepository,
48
        array $enabledTypes,
49
        LoggerInterface $logger
50
    ) {
51
        $this->secondFactorRepository = $secondFactorRepository;
52
        $this->enabledTypes = array_combine($enabledTypes, $enabledTypes);
53
        $this->logger = $logger;
54
    }
55
56
    public function getAllMatchingFor(Loa $highestLoa, $identityNameId)
57
    {
58
        $enabledSecondFactors = new ArrayCollection();
59
60
        foreach ($this->secondFactorRepository->getAllMatchingFor($highestLoa, $identityNameId) as $secondFactor) {
61
            if (!array_key_exists($secondFactor->secondFactorType, $this->enabledTypes)) {
62
                $this->logger->info(
63
                    sprintf(
64
                        'Discarding second factor; its second factor type "%s" is not enabled',
65
                        $secondFactor->secondFactorType
66
                    )
67
                );
68
69
                continue;
70
            }
71
72
            $enabledSecondFactors->add($secondFactor);
73
        }
74
75
        return $enabledSecondFactors;
76
    }
77
78
    public function findOneBySecondFactorId($secondFactorId)
79
    {
80
        $secondFactor = $this->secondFactorRepository->findOneBySecondFactorId($secondFactorId);
81
82
        if (!$secondFactor || !array_key_exists($secondFactor->secondFactorType, $this->enabledTypes)) {
83
            return null;
84
        }
85
86
        return $secondFactor;
87
    }
88
}
89