Failed Conditions
Push — v7 ( 0b17f0...b3d8c9 )
by Florent
02:08
created

ClaimCheckerManagerFactory::checkers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
use Jose\Component\Core\Converter\JsonConverterInterface;
17
18
/**
19
 * Class ClaimCheckerManagerFactory.
20
 */
21
final class ClaimCheckerManagerFactory
22
{
23
    /**
24
     * @var ClaimCheckerInterface[]
25
     */
26
    private $checkers = [];
27
    /**
28
     * @var JsonConverterInterface
29
     */
30
    private $jsonConverter;
31
32
    /**
33
     * ClaimCheckerManager constructor.
34
     *
35
     * @param JsonConverterInterface $jsonConverter
36
     */
37
    public function __construct(JsonConverterInterface $jsonConverter)
38
    {
39
        $this->jsonConverter = $jsonConverter;
40
    }
41
42
    /**
43
     * @param string[] $aliases
44
     *
45
     * @return ClaimCheckerManager
46
     */
47
    public function create(array $aliases): ClaimCheckerManager
48
    {
49
        $checkers = [];
50
        foreach ($aliases as $alias) {
51
            if (array_key_exists($alias, $this->checkers)) {
52
                $checkers[] = $this->checkers[$alias];
53
            } else {
54
                throw new \InvalidArgumentException(sprintf('The claim checker with the alias "%s" is not supported.', $alias));
55
            }
56
        }
57
58
        return new ClaimCheckerManager($this->jsonConverter, $checkers);
59
    }
60
61
    /**
62
     * @param string                $alias
63
     * @param ClaimCheckerInterface $checker
64
     *
65
     * @return ClaimCheckerManagerFactory
66
     */
67
    public function add(string $alias, ClaimCheckerInterface $checker): ClaimCheckerManagerFactory
68
    {
69
        if (array_key_exists($alias, $this->checkers)) {
70
            throw new \InvalidArgumentException(sprintf('The alias "%s" already exists.', $alias));
71
        }
72
        $this->checkers[$alias] = $checker;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return string[]
79
     */
80
    public function aliases(): array
81
    {
82
        return array_keys($this->checkers);
83
    }
84
85
    /**
86
     * @return ClaimCheckerInterface[]
87
     */
88
    public function checkers(): array
89
    {
90
        return $this->checkers;
91
    }
92
}
93