Completed
Push — feature/sf-subset/middleware-a... ( c90b65...4cd7d0 )
by A.
04:33
created

AllowedSecondFactorMap::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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 $mappedAllowedSecondFactors = [];
31
32
    private function __construct()
33
    {
34
    }
35
36
    /**
37
     * @param AllowedSecondFactor[] $allowedSecondFactors
38
     * @return AllowedSecondFactorMap
39
     */
40
    public static function from($allowedSecondFactors)
41
    {
42
        $allowedSecondFactorMap = new self();
43
        foreach ($allowedSecondFactors as $allowedSecondFactor) {
44
            $allowedSecondFactorMap->add($allowedSecondFactor);
45
        }
46
47
        return $allowedSecondFactorMap;
48
    }
49
50
    /**
51
     * @param Institution $institution
52
     * @return AllowedSecondFactorList
53
     */
54
    public function getAllowedSecondFactorListFor(Institution $institution)
55
    {
56
        $institution = strtolower($institution->getInstitution());
57
        if (!array_key_exists($institution, $this->mappedAllowedSecondFactors)) {
58
            return AllowedSecondFactorList::blank();
59
        }
60
61
        return AllowedSecondFactorList::ofTypes($this->mappedAllowedSecondFactors[$institution]);
62
    }
63
64
    /**
65
     * @param AllowedSecondFactor $allowedSecondFactor
66
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
67
     */
68
    private function add(AllowedSecondFactor $allowedSecondFactor)
69
    {
70
        $institution = strtolower($allowedSecondFactor->institution->getInstitution());
71
72
        $this->mappedAllowedSecondFactors[$institution][] = $allowedSecondFactor->secondFactorType;
73
    }
74
}
75