Pipeline   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
eloc 19
c 3
b 1
f 0
dl 0
loc 72
ccs 18
cts 18
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 7 2
A __construct() 0 3 1
A chainMiddleware() 0 12 1
A withRequest() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot;
5
6
use Psr\Http\Message\ServerRequestInterface;
7
8
/**
9
 * The processing pipeline of Shoot. Holds the middleware that enables Shoot's functionality. It's called from the Twig
10
 * extension.
11
 */
12
final class Pipeline
13
{
14
    /** @var callable */
15
    private $middleware;
16
17
    /** @var ServerRequestInterface|null */
18
    private $request;
19
20
    /**
21
     * Constructs an instance of Pipeline. Takes the middleware that enables Shoot's functionality. Middleware is
22
     * executed in the same order as given.
23
     *
24
     * @param MiddlewareInterface[] $middleware
25
     */
26 20
    public function __construct(array $middleware = [])
27
    {
28 20
        $this->middleware = $this->chainMiddleware($middleware);
29 20
    }
30
31
    /**
32
     * Sets the HTTP request context while executing the given callback. Any templates should be rendered within this
33
     * callback. Returns the result returned by the callback (if any).
34
     *
35
     * @param ServerRequestInterface $request
36
     * @param callable               $callback
37
     *
38
     * @return mixed
39
     */
40 17
    public function withRequest(ServerRequestInterface $request, callable $callback)
41
    {
42
        try {
43 17
            $this->request = $request;
44
45 17
            return $callback();
46
        } finally {
47 17
            $this->request = null;
48
        }
49
    }
50
51
    /**
52
     * @param View $view
53
     *
54
     * @return void
55
     *
56
     * @internal
57
     */
58 17
    public function process(View $view): void
59
    {
60 17
        if ($this->request === null) {
61 1
            throw new MissingRequestException('Cannot process a view without a request set. This method should be called from the callback passed to Pipeline::withRequest');
62
        }
63
64 16
        call_user_func($this->middleware, $view);
65 14
    }
66
67
    /**
68
     * @param MiddlewareInterface[] $middleware
69
     *
70
     * @return callable
71
     */
72 20
    private function chainMiddleware(array $middleware): callable
73
    {
74 20
        $middleware = array_reverse($middleware);
75
76
        return array_reduce($middleware, function (callable $next, MiddlewareInterface $middleware): callable {
77
            return function (View $view) use ($middleware, $next): View {
78 15
                return $middleware->process($view, $this->request, $next);
0 ignored issues
show
Bug introduced by
It seems like $this->request can also be of type null; however, parameter $request of Shoot\Shoot\MiddlewareInterface::process() does only seem to accept Psr\Http\Message\ServerRequestInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                return $middleware->process($view, /** @scrutinizer ignore-type */ $this->request, $next);
Loading history...
79 16
            };
80
        }, function (View $view): View {
81 12
            $view->render();
82
83 10
            return $view;
84 20
        });
85
    }
86
}
87