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

findOneBySecondFactorId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 2
nop 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\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
    /**
38
     * @var LoggerInterface
39
     */
40
    private $logger;
41
42
    /**
43
     * @param SecondFactorRepository $secondFactorRepository
44
     * @param string[]               $enabledTypes
45
     * @param LoggerInterface        $logger
46
     */
47
    public function __construct(
48
        SecondFactorRepository $secondFactorRepository,
49
        array $enabledTypes,
50
        LoggerInterface $logger
51
    ) {
52
        $this->secondFactorRepository = $secondFactorRepository;
53
        $this->enabledTypes = array_combine($enabledTypes, $enabledTypes);
54
        $this->logger = $logger;
55
    }
56
57
    public function getAllMatchingFor(Loa $highestLoa, $identityNameId)
58
    {
59
        $enabledSecondFactors = new ArrayCollection();
60
61
        foreach ($this->secondFactorRepository->getAllMatchingFor($highestLoa, $identityNameId) as $secondFactor) {
62
            if (!array_key_exists($secondFactor->secondFactorType, $this->enabledTypes)) {
63
                $this->logger->info(
64
                    sprintf(
65
                        'Discarding second factor; its second factor type "%s" is not enabled',
66
                        $secondFactor->secondFactorType
67
                    )
68
                );
69
70
                continue;
71
            }
72
73
            $enabledSecondFactors->add($secondFactor);
74
        }
75
76
        return $enabledSecondFactors;
77
    }
78
79
    public function findOneBySecondFactorId($secondFactorId)
80
    {
81
        $secondFactor = $this->secondFactorRepository->findOneBySecondFactorId($secondFactorId);
82
83
        if (!$secondFactor || !array_key_exists($secondFactor->secondFactorType, $this->enabledTypes)) {
84
            return null;
85
        }
86
87
        return $secondFactor;
88
    }
89
}
90