Passed
Pull Request — master (#1168)
by Aleksei
12:55
created

PipelineTrait::makePipeline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 10
cc 2
nc 2
nop 0
crap 2.0932
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\LazyPipeline;
11
use Spiral\Http\Pipeline;
12
use Spiral\Router\Exception\RouteException;
13
use Spiral\Router\PipelineFactory;
14
use Spiral\Router\RouteInterface;
15
16
/**
17
 * @psalm-type MiddlewareType = MiddlewareInterface|class-string<MiddlewareInterface>|non-empty-string|Autowire
18
 */
19
trait PipelineTrait
20
{
21
    use ContainerTrait;
22
23
    protected Pipeline|LazyPipeline|null $pipeline = null;
24
25
    /** @psalm-var array<array-key, MiddlewareType> */
26
    protected array $middleware = [];
27
28
    /**
29
     * Associated middleware with route. New instance of route will be returned.
30
     *
31
     * Example:
32
     * $route->withMiddleware(new CacheMiddleware(100));
33
     * $route->withMiddleware(ProxyMiddleware::class);
34
     * $route->withMiddleware(ProxyMiddleware::class, OtherMiddleware::class);
35
     * $route->withMiddleware([ProxyMiddleware::class, OtherMiddleware::class]);
36
     *
37
     * @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...
38
     * @return RouteInterface|$this
39
     *
40
     * @throws RouteException
41
     */
42 393
    public function withMiddleware(...$middleware): RouteInterface
43
    {
44 393
        $route = clone $this;
45
46
        // array fallback
47 393
        if (\count($middleware) === 1 && \is_array($middleware[0])) {
48 4
            $middleware = $middleware[0];
49
        }
50
51
        /** @var MiddlewareType[] $middleware */
52 393
        foreach ($middleware as $item) {
53 371
            $route->middleware[] = $item;
54
        }
55
56 393
        if ($route->pipeline !== null) {
57 5
            $route->pipeline = $route->makeLazyPipeline();
58
        }
59
60 393
        return $route;
61
    }
62
63
    public function withPipeline(Pipeline $pipeline): static
64
    {
65
        $route = clone $this;
66
67
        $route->middleware = [$pipeline];
68
        $route->pipeline = $pipeline;
69
70
        return $route;
71
    }
72
73
    /**
74
     * Get associated route pipeline.
75
     *
76
     * @throws RouteException
77
     *
78
     * @deprecated Will be removed in Spiral v4.0. Use {@see makeLazyPipeline()} instead.
79
     */
80 3
    protected function makePipeline(): Pipeline
81
    {
82 3
        \assert($this->container !== null);
83
        try {
84 3
            return $this->container
85 3
                ->get(PipelineFactory::class)
86 3
                ->createWithMiddleware($this->middleware);
87
        } catch (ContainerExceptionInterface $e) {
88
            throw new RouteException($e->getMessage(), $e->getCode(), $e);
89
        }
90
    }
91
92
    /**
93
     * Get associated route pipeline.
94
     *
95
     * @throws RouteException
96
     */
97 438
    protected function makeLazyPipeline(): LazyPipeline
98
    {
99 438
        \assert($this->container !== null);
100
        try {
101
            /** @var LazyPipeline $pipeline */
102 438
            $pipeline = $this->container->get(LazyPipeline::class);
103 438
            return $pipeline->withMiddleware(...$this->middleware);
104 2
        } catch (ContainerExceptionInterface $e) {
105
            throw new RouteException($e->getMessage(), $e->getCode(), $e);
106
        }
107
    }
108
}
109