Failed Conditions
Push — master ( 2f569d...788569 )
by Florent
01:53
created

ClaimCheckerManagerFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Checker;
15
16
/**
17
 * Class ClaimCheckerManagerFactory.
18
 */
19
final class ClaimCheckerManagerFactory
20
{
21
    /**
22
     * @var ClaimCheckerInterface[]
23
     */
24
    private $checkers = [];
25
26
    /**
27
     * @param string[] $aliases
28
     *
29
     * @return ClaimCheckerManager
30
     */
31
    public function create(array $aliases): ClaimCheckerManager
32
    {
33
        $checkers = [];
34
        foreach ($aliases as $alias) {
35
            if (array_key_exists($alias, $this->checkers)) {
36
                $checkers[] = $this->checkers[$alias];
37
            } else {
38
                throw new \InvalidArgumentException(sprintf('The claim checker with the alias "%s" is not supported.', $alias));
39
            }
40
        }
41
42
        return ClaimCheckerManager::create($checkers);
43
    }
44
45
    /**
46
     * @param string                $alias
47
     * @param ClaimCheckerInterface $checker
48
     *
49
     * @return ClaimCheckerManagerFactory
50
     */
51
    public function add(string $alias, ClaimCheckerInterface $checker): ClaimCheckerManagerFactory
52
    {
53
        if (array_key_exists($alias, $this->checkers)) {
54
            throw new \InvalidArgumentException(sprintf('The alias "%s" already exists.', $alias));
55
        }
56
        $this->checkers[$alias] = $checker;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return string[]
63
     */
64
    public function aliases(): array
65
    {
66
        return array_keys($this->checkers);
67
    }
68
69
    /**
70
     * @return ClaimCheckerInterface[]
71
     */
72
    public function checkers(): array
73
    {
74
        return $this->checkers;
75
    }
76
}
77