ServiceLocatorMiddlewareRegistry   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 49
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A byName() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SymfonyMiddleware\Middleware\Registry;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Kafkiansky\SymfonyMiddleware\Middleware\MiddlewareNotConfigured;
10
11
final class ServiceLocatorMiddlewareRegistry implements MiddlewareRegistry
12
{
13
    private ContainerInterface $container;
14
15
    /**
16
     * @var array<string, array{if?: bool, middlewares: class-string<MiddlewareInterface>[]}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, array{if?:...iddlewareInterface>[]}> at position 12 could not be parsed: Unknown type name 'class-string' at position 12 in array<string, array{if?: bool, middlewares: class-string<MiddlewareInterface>[]}>.
Loading history...
17
     */
18
    private array $groups;
19
20
    /**
21
     * @psalm-param array<string, array{if?: bool, middlewares: class-string<MiddlewareInterface>[]}> $groups
22
     */
23
    public function __construct(ContainerInterface $container, array $groups)
24
    {
25
        $this->container = $container;
26
        $this->groups = $groups;
27
    }
28
29
    /**
30
     * @param class-string<MiddlewareInterface>|string $middlewareFqcnOrGroup
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<MiddlewareInterface>|string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<MiddlewareInterface>|string.
Loading history...
31
     *
32
     * @throws MiddlewareNotConfigured
33
     *
34
     * @return MiddlewareInterface[]
35
     */
36
    public function byName(string $middlewareFqcnOrGroup): array
37
    {
38
        if ($this->container->has($middlewareFqcnOrGroup) === false && !isset($this->groups[$middlewareFqcnOrGroup])) {
39
            throw MiddlewareNotConfigured::forMiddleware($middlewareFqcnOrGroup);
40
        }
41
42
        if ($this->container->has($middlewareFqcnOrGroup)) {
43
            /** @var MiddlewareInterface[] */
44
            return [$this->container->get($middlewareFqcnOrGroup)];
45
        }
46
47
        /** @var MiddlewareInterface[] $middlewares */
48
        $middlewares = [];
49
50
        if (!isset($this->groups[$middlewareFqcnOrGroup]['if']) || $this->groups[$middlewareFqcnOrGroup]['if']) {
51
            $middlewares = array_map(function (string $middlewareFqcn): MiddlewareInterface {
52
                /** @var MiddlewareInterface */
53
                return $this->container->get($middlewareFqcn);
54
            }, $this->groups[$middlewareFqcnOrGroup]['middlewares']
55
                ?? throw MiddlewareNotConfigured::becauseGroupIsEmpty($middlewareFqcnOrGroup)
56
            );
57
        }
58
59
        return $middlewares;
60
    }
61
}
62