Passed
Push — master ( 45ab73...eabe48 )
by Victor
45s queued 10s
created

Pipeline::addSuppressionMiddleware()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot;
5
6
use Psr\Http\Message\ServerRequestInterface;
7
use Shoot\Shoot\Middleware\SuppressionMiddleware;
8
9
/**
10
 * The processing pipeline of Shoot. Holds the middleware that enables Shoot's functionality. It's called from the Twig
11
 * extension.
12
 */
13
final class Pipeline
14
{
15
    /** @var callable */
16
    private $middleware;
17
18
    /** @var ServerRequestInterface|null */
19
    private $request;
20
21
    /**
22
     * Constructs an instance of Pipeline. Takes the middleware that enables Shoot's functionality. Middleware is
23
     * executed in the same order as given.
24
     *
25
     * @param MiddlewareInterface[] $middleware
26
     */
27 20
    public function __construct(array $middleware = [])
28
    {
29 20
        $this->middleware = $this->chainMiddleware($middleware);
30 20
    }
31
32
    /**
33
     * Sets the HTTP request context while executing the given callback. Any templates should be rendered within this
34
     * callback. Returns the result returned by the callback (if any).
35
     *
36
     * @param ServerRequestInterface $request
37
     * @param callable               $callback
38
     *
39
     * @return mixed
40
     */
41 17
    public function withRequest(ServerRequestInterface $request, callable $callback)
42
    {
43
        try {
44 17
            $this->request = $request;
45
46 17
            return $callback();
47
        } finally {
48 17
            $this->request = null;
49
        }
50
    }
51
52
    /**
53
     * @param View $view
54
     *
55
     * @return void
56
     *
57
     * @internal
58
     */
59 17
    public function process(View $view): void
60
    {
61 17
        if ($this->request === null) {
62 1
            throw new MissingRequestException('Cannot process a view without a request set. This method should be called from the callback passed to Pipeline::withRequest');
63
        }
64
65 16
        call_user_func($this->middleware, $view);
66 14
    }
67
68
    /**
69
     * @param MiddlewareInterface[] $middleware
70
     *
71
     * @return callable
72
     */
73 20
    private function chainMiddleware(array $middleware): callable
74
    {
75 20
        $middleware = array_reverse($middleware);
76
77
        return array_reduce($middleware, function (callable $next, MiddlewareInterface $middleware): callable {
78
            return function (View $view) use ($middleware, $next): View {
79 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

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