Passed
Push — master ( f5a1ef...a14569 )
by butschster
29:16 queued 19:38
created

PipelineTrait::makePipeline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Router\Traits;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Spiral\Core\Container\Autowire;
10
use Spiral\Http\Pipeline;
11
use Spiral\Router\Exception\RouteException;
12
use Spiral\Router\PipelineFactory;
13
use Spiral\Router\RouteInterface;
14
15
/**
16
 * @psalm-type MiddlewareType = MiddlewareInterface|class-string<MiddlewareInterface>|non-empty-string|Autowire
17
 */
18
trait PipelineTrait
19
{
20
    use ContainerTrait;
21
22
    protected ?Pipeline $pipeline = null;
23
24
    /** @psalm-var array<array-key, MiddlewareType> */
25
    protected array $middleware = [];
26
27
    /**
28
     * Associated middleware with route. New instance of route will be returned.
29
     *
30
     * Example:
31
     * $route->withMiddleware(new CacheMiddleware(100));
32
     * $route->withMiddleware(ProxyMiddleware::class);
33
     * $route->withMiddleware(ProxyMiddleware::class, OtherMiddleware::class);
34
     * $route->withMiddleware([ProxyMiddleware::class, OtherMiddleware::class]);
35
     *
36
     * @param MiddlewareType|array{0:MiddlewareType[]} ...$middleware
0 ignored issues
show
Bug introduced by
The type Spiral\Router\Traits\MiddlewareType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     * @return RouteInterface|$this
38
     *
39
     * @throws RouteException
40
     */
41 315
    public function withMiddleware(...$middleware): RouteInterface
42
    {
43 315
        $route = clone $this;
44
45
        // array fallback
46 315
        if (\count($middleware) === 1 && \is_array($middleware[0])) {
47 4
            $middleware = $middleware[0];
48
        }
49
50
        /** @var MiddlewareType[] $middleware */
51 315
        foreach ($middleware as $item) {
52 302
            $route->middleware[] = $item;
53
        }
54
55 315
        if ($route->pipeline !== null) {
56 5
            $route->pipeline = $route->makePipeline();
57
        }
58
59 315
        return $route;
60
    }
61
62
    public function withPipeline(Pipeline $pipeline): static
63
    {
64
        $route = clone $this;
65
66
        $route->middleware = [$pipeline];
67
        $route->pipeline = $pipeline;
68
69
        return $route;
70
    }
71
72
    /**
73
     * Get associated route pipeline.
74
     *
75
     * @throws RouteException
76
     */
77 354
    protected function makePipeline(): Pipeline
78
    {
79 354
        \assert($this->container !== null);
80
        try {
81 354
            return $this->container
82 354
                ->get(PipelineFactory::class)
83 354
                ->createWithMiddleware($this->middleware);
84 3
        } catch (ContainerExceptionInterface $e) {
85 1
            throw new RouteException($e->getMessage(), $e->getCode(), $e);
86
        }
87
    }
88
}
89