AlgorithmManagerFactoryFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 38
ccs 14
cts 15
cp 0.9333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 3
A getAlgorithms() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\JWTModule\DIFactory\Core;
6
7
use Jose\Component\Core\Algorithm;
8
use Jose\Component\Core\AlgorithmManagerFactory;
9
use Psr\Container\ContainerInterface;
10
use TMV\JWTModule\Exception\InvalidArgumentException;
11
12
class AlgorithmManagerFactoryFactory
13
{
14 18
    public function __invoke(ContainerInterface $container): AlgorithmManagerFactory
15
    {
16 18
        $config = $container->get('config')['jwt_module']['algorithm_manager'] ?? [];
17
18 18
        $algorithmManagerFactory = new AlgorithmManagerFactory();
19
20 18
        $algorithms = $this->getAlgorithms($container, $config['algorithms'] ?? []);
21
22 18
        foreach ($algorithms as $alias => $algorithm) {
23 18
            $algorithmManagerFactory->add(! \is_string($alias) ? $algorithm->name() : $alias, $algorithm);
24
        }
25
26 18
        return $algorithmManagerFactory;
27
    }
28
29
    /**
30
     * @param ContainerInterface $container
31
     * @param string[]|Algorithm[] $algorithms
32
     *
33
     * @return Algorithm[]
34
     */
35 18
    private function getAlgorithms(ContainerInterface $container, array $algorithms): array
36
    {
37
        return \array_map(static function ($algorithm) use ($container) {
38 18
            if ($algorithm instanceof Algorithm) {
39 1
                return $algorithm;
40
            }
41
42 18
            $algorithm = $container->get($algorithm);
43
44 18
            if (! $algorithm instanceof Algorithm) {
45
                throw new InvalidArgumentException('Invalid algorithm');
46
            }
47
48 18
            return $algorithm;
49 18
        }, $algorithms);
50
    }
51
}
52