Completed
Pull Request — develop (#92)
by Boy
02:59
created

findAuthnContextClassRefByLoa()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
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 9
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace Surfnet\StepupGateway\GatewayBundle\Service;
4
5
use Surfnet\StepupBundle\Value\Loa;
6
use Surfnet\StepupGateway\GatewayBundle\Exception\InvalidArgumentException;
7
8
final class AuthnContextClassRefLookupService
9
{
10
    /**
11
     * @var array<string,string>
12
     */
13
    private $loaAuthnContextClassMapping;
14
15
    /**
16
     * @param array<string,string> $loaAuthnContextClassMapping
17
     */
18
    public function __construct(array $loaAuthnContextClassMapping)
19
    {
20
        foreach ($loaAuthnContextClassMapping as $loaId => $authnContextClassRef) {
21
            if (!is_string($loaId)) {
22
                throw InvalidArgumentException::invalidType(
23
                    'string',
24
                    'authnContextClassRef',
25
                    $authnContextClassRef
26
                );
27
            }
28
            if (!is_string($authnContextClassRef)) {
29
                throw InvalidArgumentException::invalidType(
30
                    'string',
31
                    'ref',
32
                    $authnContextClassRef
33
                );
34
            }
35
        }
36
37
        $this->loaAuthnContextClassMapping = $loaAuthnContextClassMapping;
38
    }
39
40
    /**
41
     * @param string $authnContextClassRef
42
     * @return string|bool
43
     */
44
    public function findLoaIdByAuthnContextClassRef($authnContextClassRef)
45
    {
46
        if (!is_string($authnContextClassRef)) {
47
            throw InvalidArgumentException::invalidType(
48
                'string',
49
                'authnContextClassRef',
50
                $authnContextClassRef
51
            );
52
        }
53
        return array_search($authnContextClassRef, $this->loaAuthnContextClassMapping);
54
    }
55
56
    /**
57
     * @param Loa $loa
58
     * @return string|bool
59
     */
60
    public function findAuthnContextClassRefByLoa(Loa $loa)
61
    {
62
        foreach ($this->loaAuthnContextClassMapping as $loaId => $ref) {
63
            if ($loa->isIdentifiedBy($loaId)) {
64
                return $ref;
65
            }
66
        }
67
        return false;
68
    }
69
}
70