1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 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\Bundle\JoseFramework\Services; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Jose\Component\Checker\HeaderChecker; |
18
|
|
|
use Jose\Component\Checker\TokenTypeSupport; |
19
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
|
21
|
|
|
final class HeaderCheckerManagerFactory |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var EventDispatcherInterface |
25
|
|
|
*/ |
26
|
|
|
private $eventDispatcher; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var HeaderChecker[] |
30
|
|
|
*/ |
31
|
|
|
private $checkers = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TokenTypeSupport[] |
35
|
|
|
*/ |
36
|
|
|
private $tokenTypes = []; |
37
|
|
|
|
38
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
39
|
|
|
{ |
40
|
|
|
$this->eventDispatcher = $eventDispatcher; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This method creates a Header Checker Manager and populate it with the header parameter checkers found based on the alias. |
45
|
|
|
* If the alias is not supported, an InvalidArgumentException is thrown. |
46
|
|
|
* |
47
|
|
|
* @param string[] $aliases |
48
|
|
|
* |
49
|
|
|
* @throws InvalidArgumentException if an alias is not supported |
50
|
|
|
*/ |
51
|
|
|
public function create(array $aliases): HeaderCheckerManager |
52
|
|
|
{ |
53
|
|
|
$checkers = []; |
54
|
|
|
foreach ($aliases as $alias) { |
55
|
|
|
if (!isset($this->checkers[$alias])) { |
56
|
|
|
throw new InvalidArgumentException(sprintf('The header checker with the alias "%s" is not supported.', $alias)); |
57
|
|
|
} |
58
|
|
|
$checkers[] = $this->checkers[$alias]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new HeaderCheckerManager($checkers, $this->tokenTypes, $this->eventDispatcher); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This method adds a header parameter checker to this factory. |
66
|
|
|
* The checker is uniquely identified by an alias. This allows the same header parameter checker to be added twice (or more) |
67
|
|
|
* using several configuration options. |
68
|
|
|
*/ |
69
|
|
|
public function add(string $alias, HeaderChecker $checker): void |
70
|
|
|
{ |
71
|
|
|
$this->checkers[$alias] = $checker; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This method adds a token type support to this factory. |
76
|
|
|
*/ |
77
|
|
|
public function addTokenTypeSupport(TokenTypeSupport $tokenType): void |
78
|
|
|
{ |
79
|
|
|
$this->tokenTypes[] = $tokenType; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns all header parameter checker aliases supported by this factory. |
84
|
|
|
* |
85
|
|
|
* @return string[] |
86
|
|
|
*/ |
87
|
|
|
public function aliases(): array |
88
|
|
|
{ |
89
|
|
|
return array_keys($this->checkers); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns all header parameter checkers supported by this factory. |
94
|
|
|
* |
95
|
|
|
* @return HeaderChecker[] |
96
|
|
|
*/ |
97
|
|
|
public function all(): array |
98
|
|
|
{ |
99
|
|
|
return $this->checkers; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|