ClaimCheckerManagerFactoryFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 1
b 0
f 0
dl 0
loc 46
ccs 15
cts 17
cp 0.8824
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 17 3
A getCheckers() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\JWTModule\DIFactory\Checker;
6
7
use Jose\Component\Checker\ClaimChecker;
8
use Jose\Component\Checker\ClaimCheckerManagerFactory;
9
use Psr\Container\ContainerInterface;
10
use TMV\JWTModule\Exception\InvalidArgumentException;
11
12
class ClaimCheckerManagerFactoryFactory
13
{
14
    private const MODULE_KEY = 'jwt_module';
15
16
    private const SERVICE_TYPE_KEY = 'claim_checker_manager';
17
18 3
    public function __invoke(ContainerInterface $container): ClaimCheckerManagerFactory
19
    {
20 3
        $config = $container->get('config')[static::MODULE_KEY][static::SERVICE_TYPE_KEY] ?? [];
21
22 3
        $checkers = $this->getCheckers($container, $config['checkers'] ?? []);
23
24 3
        $factory = new ClaimCheckerManagerFactory();
25
26 3
        foreach ($checkers as $alias => $checker) {
27 3
            if (! \is_string($alias)) {
28
                throw new InvalidArgumentException('Invalid alias for claim checker');
29
            }
30
31 3
            $factory->add($alias, $checker);
32
        }
33
34 3
        return $factory;
35
    }
36
37
    /**
38
     * @param ContainerInterface $container
39
     * @param string[]|ClaimChecker[] $checkers
40
     *
41
     * @return ClaimChecker[]
42
     */
43 3
    private function getCheckers(ContainerInterface $container, array $checkers): array
44
    {
45
        return \array_map(static function ($checker) use ($container) {
46 3
            if ($checker instanceof ClaimChecker) {
47 1
                return $checker;
48
            }
49
50 3
            $checker = $container->get($checker);
51
52 3
            if (! $checker instanceof ClaimChecker) {
53
                throw new InvalidArgumentException('Invalid claim checker');
54
            }
55
56 3
            return $checker;
57 3
        }, $checkers);
58
    }
59
}
60