Completed
Push — feature/second-factor-only-2 ( 838e43 )
by Boy
03:36
created

LoaAliasLookupService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A findLoaIdByAlias() 0 11 2
A findAliasByLoa() 0 9 3
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\SecondFactorOnlyBundle\Service;
20
21
use Surfnet\StepupBundle\Value\Loa;
22
use Surfnet\StepupGateway\GatewayBundle\Exception\InvalidArgumentException;
23
24
final class LoaAliasLookupService
25
{
26
    /**
27
     * @var array<string,string>
28
     */
29
    private $loaAliases;
30
31
    /**
32
     * @param array<string,string> $loaAliases
33
     */
34
    public function __construct(array $loaAliases)
35
    {
36
        foreach ($loaAliases as $loaId => $alias) {
37
            if (!is_string($loaId)) {
38
                throw InvalidArgumentException::invalidType(
39
                    'string',
40
                    'loaId',
41
                    $alias
42
                );
43
            }
44
            if (!is_string($alias)) {
45
                throw InvalidArgumentException::invalidType(
46
                    'string',
47
                    'alias',
48
                    $alias
49
                );
50
            }
51
        }
52
53
        $this->loaAliases = $loaAliases;
54
    }
55
56
    /**
57
     * @param string $alias
58
     * @return string|bool
59
     */
60
    public function findLoaIdByAlias($alias)
61
    {
62
        if (!is_string($alias)) {
63
            throw InvalidArgumentException::invalidType(
64
                'string',
65
                'alias',
66
                $alias
67
            );
68
        }
69
        return array_search($alias, $this->loaAliases);
70
    }
71
72
    /**
73
     * @param Loa $loa
74
     * @return string|bool
75
     */
76
    public function findAliasByLoa(Loa $loa)
77
    {
78
        foreach ($this->loaAliases as $loaId => $alias) {
79
            if ($loa->isIdentifiedBy($loaId)) {
80
                return $alias;
81
            }
82
        }
83
        return false;
84
    }
85
}
86