Middlewares::group()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
eloc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router\Configure;
6
7
use Gacela\Router\Middleware\MiddlewareInterface;
8
9
final class Middlewares
10
{
11
    /** @var list<MiddlewareInterface|class-string<MiddlewareInterface>|string> */
12
    private array $globalMiddlewares = [];
13
14
    /** @var array<string, list<MiddlewareInterface|class-string<MiddlewareInterface>>> */
15
    private array $groups = [];
16
17
    /**
18
     * @param MiddlewareInterface|class-string<MiddlewareInterface>|string $middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment MiddlewareInterface|clas...lewareInterface>|string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in MiddlewareInterface|class-string<MiddlewareInterface>|string.
Loading history...
19
     */
20
    public function add(MiddlewareInterface|string $middleware): self
21
    {
22
        $this->globalMiddlewares[] = $middleware;
23
        return $this;
24
    }
25
26
    /**
27
     * @param list<MiddlewareInterface|class-string<MiddlewareInterface>> $middlewares
28
     */
29
    public function group(string $name, array $middlewares): self
30
    {
31
        $this->groups[$name] = $middlewares;
32
        return $this;
33
    }
34
35
    /**
36
     * @return list<MiddlewareInterface|class-string<MiddlewareInterface>|string>
37
     */
38
    public function getAll(): array
39
    {
40
        return $this->globalMiddlewares;
41
    }
42
43
    /**
44
     * @return array<string, list<MiddlewareInterface|class-string<MiddlewareInterface>>>
45
     */
46
    public function getGroups(): array
47
    {
48
        return $this->groups;
49
    }
50
51
    /**
52
     * @param MiddlewareInterface|class-string<MiddlewareInterface>|string $middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment MiddlewareInterface|clas...lewareInterface>|string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in MiddlewareInterface|class-string<MiddlewareInterface>|string.
Loading history...
53
     *
54
     * @return list<MiddlewareInterface|class-string<MiddlewareInterface>>
55
     */
56
    public function resolve(MiddlewareInterface|string $middleware): array
57
    {
58
        // If it's already an instance, return it as-is
59
        if ($middleware instanceof MiddlewareInterface) {
60
            return [$middleware];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($middleware) returns the type array<integer,Gacela\Rou...re\MiddlewareInterface> which is incompatible with the documented return type Gacela\Router\Configure\list.
Loading history...
61
        }
62
63
        // If it's a group name, return the group's middlewares
64
        if (isset($this->groups[$middleware])) {
65
            return $this->groups[$middleware];
66
        }
67
68
        // Otherwise, treat it as a class string
69
        /** @var class-string<MiddlewareInterface> $middleware */
70
        return [$middleware];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($middleware) returns the type array<integer,string> which is incompatible with the documented return type Gacela\Router\Configure\list.
Loading history...
71
    }
72
}
73