Failed Conditions
Push — master ( a80032...387aac )
by Florent
02:22
created

HeaderCheckerManagerFactory::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
/**
17
 * Class HeaderCheckerManagerFactory.
18
 */
19
final class HeaderCheckerManagerFactory
20
{
21
    /**
22
     * @var HeaderChecker[]
23
     */
24
    private $checkers = [];
25
26
    /**
27
     * @var TokenTypeSupport[]
28
     */
29
    private $tokenTypes = [];
30
31
    /**
32
     * @param string[] $aliases
33
     *
34
     * @return HeaderCheckerManager
35
     */
36
    public function create(array $aliases): HeaderCheckerManager
37
    {
38
        $checkers = [];
39
        foreach ($aliases as $alias) {
40
            if (array_key_exists($alias, $this->checkers)) {
41
                $checkers[] = $this->checkers[$alias];
42
            } else {
43
                throw new \InvalidArgumentException(sprintf('The header checker with the alias "%s" is not supported.', $alias));
44
            }
45
        }
46
47
        return HeaderCheckerManager::create($checkers, $this->tokenTypes);
48
    }
49
50
    /**
51
     * @param string                 $alias
52
     * @param HeaderChecker $checker
53
     *
54
     * @return HeaderCheckerManagerFactory
55
     */
56
    public function add(string $alias, HeaderChecker $checker): self
57
    {
58
        $this->checkers[$alias] = $checker;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param TokenTypeSupport $tokenType
65
     *
66
     * @return HeaderCheckerManagerFactory
67
     */
68
    public function addTokenTypeSupport(TokenTypeSupport $tokenType): self
69
    {
70
        $this->tokenTypes[] = $tokenType;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return string[]
77
     */
78
    public function aliases(): array
79
    {
80
        return array_keys($this->checkers);
81
    }
82
}
83