AllowedSecondFactorMap   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 8 2
A __construct() 0 2 1
A getAllowedSecondFactorListFor() 0 8 2
A add() 0 5 1
1
<?php
2
3
/**
4
 * Copyright 2017 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\StepupMiddleware\ApiBundle\Configuration\Service;
20
21
use Surfnet\Stepup\Configuration\Value\AllowedSecondFactorList;
22
use Surfnet\Stepup\Configuration\Value\Institution;
23
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\AllowedSecondFactor;
24
25
final class AllowedSecondFactorMap
26
{
27
    /**
28
     * @var AllowedSecondFactor[]
29
     */
30
    private array $mappedAllowedSecondFactors = [];
31
32
    private function __construct()
33
    {
34
    }
35
36
    /**
37
     * @param AllowedSecondFactor[] $allowedSecondFactors
38
     * @return AllowedSecondFactorMap
39
     */
40
    public static function from(array $allowedSecondFactors): self
41
    {
42
        $allowedSecondFactorMap = new self();
43
        foreach ($allowedSecondFactors as $allowedSecondFactor) {
44
            $allowedSecondFactorMap->add($allowedSecondFactor);
45
        }
46
47
        return $allowedSecondFactorMap;
48
    }
49
50
    public function getAllowedSecondFactorListFor(Institution $institution): AllowedSecondFactorList
51
    {
52
        $institution = strtolower($institution->getInstitution());
53
        if (!array_key_exists($institution, $this->mappedAllowedSecondFactors)) {
54
            return AllowedSecondFactorList::blank();
55
        }
56
57
        return AllowedSecondFactorList::ofTypes($this->mappedAllowedSecondFactors[$institution]);
58
    }
59
60
    /**
61
     * @SuppressWarnings("PHPMD.UnusedPrivateMethod")
62
     */
63
    private function add(AllowedSecondFactor $allowedSecondFactor): void
64
    {
65
        $institution = strtolower($allowedSecondFactor->institution->getInstitution());
66
67
        $this->mappedAllowedSecondFactors[$institution][] = $allowedSecondFactor->secondFactorType;
68
    }
69
}
70