ClaimCheckerManagerFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 64
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 12 3
A add() 0 4 1
A aliases() 0 4 1
A all() 0 4 1
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\ClaimChecker;
18
use Psr\EventDispatcher\EventDispatcherInterface;
19
20
final class ClaimCheckerManagerFactory
21
{
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    private $eventDispatcher;
26
27
    /**
28
     * @var ClaimChecker[]
29
     */
30
    private $checkers = [];
31
32
    public function __construct(EventDispatcherInterface $eventDispatcher)
33
    {
34
        $this->eventDispatcher = $eventDispatcher;
35
    }
36
37
    /**
38
     * This method creates a Claim Checker Manager and populate it with the claim checkers found based on the alias.
39
     * If the alias is not supported, an InvalidArgumentException is thrown.
40
     *
41
     * @param string[] $aliases
42
     */
43
    public function create(array $aliases): ClaimCheckerManager
44
    {
45
        $checkers = [];
46
        foreach ($aliases as $alias) {
47
            if (!isset($this->checkers[$alias])) {
48
                throw new InvalidArgumentException(sprintf('The claim checker with the alias "%s" is not supported.', $alias));
49
            }
50
            $checkers[] = $this->checkers[$alias];
51
        }
52
53
        return new ClaimCheckerManager($checkers, $this->eventDispatcher);
54
    }
55
56
    /**
57
     * This method adds a claim checker to this factory.
58
     */
59
    public function add(string $alias, ClaimChecker $checker): void
60
    {
61
        $this->checkers[$alias] = $checker;
62
    }
63
64
    /**
65
     * Returns all claim checker aliases supported by this factory.
66
     *
67
     * @return string[]
68
     */
69
    public function aliases(): array
70
    {
71
        return array_keys($this->checkers);
72
    }
73
74
    /**
75
     * Returns all claim checkers supported by this factory.
76
     *
77
     * @return ClaimChecker[]
78
     */
79
    public function all(): array
80
    {
81
        return $this->checkers;
82
    }
83
}
84