Passed
Pull Request — master (#7)
by Victor
02:07
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 */
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 14
    public function __construct(array $middleware = [])
28
    {
29 14
        $middleware = $this->addSuppressionMiddleware($middleware);
0 ignored issues
show
Deprecated Code introduced by
The function Shoot\Shoot\Pipeline::addSuppressionMiddleware() has been deprecated: 2.0.0 Should not have been default behaviour. Will be removed as of the next major release. ( Ignorable by Annotation )

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

29
        $middleware = /** @scrutinizer ignore-deprecated */ $this->addSuppressionMiddleware($middleware);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
30
31 14
        $this->middleware = $this->chainMiddleware($middleware);
32 14
    }
33
34
    /**
35
     * Sets the HTTP request context while executing the given callback. Any templates should be rendered within this
36
     * callback. Returns the result returned by the callback (if any).
37
     *
38
     * @param ServerRequestInterface $request
39
     * @param callable               $callback
40
     *
41
     * @return mixed
42
     */
43 11
    public function withRequest(ServerRequestInterface $request, callable $callback)
44
    {
45
        try {
46 11
            $this->request = $request;
47
48 11
            return $callback();
49
        } finally {
50 11
            $this->request = null;
51
        }
52
    }
53
54
    /**
55
     * @param View $view
56
     *
57
     * @return void
58
     *
59
     * @internal
60
     */
61 11
    public function process(View $view)
62
    {
63 11
        if ($this->request === null) {
64 1
            throw new MissingRequestException('Cannot process a view without a request set. This method should be called from the callback passed to Pipeline::withRequest');
65
        }
66
67 10
        call_user_func($this->middleware, $view);
68 9
    }
69
70
    /**
71
     * @param MiddlewareInterface[] $middleware
72
     *
73
     * @return MiddlewareInterface[]
74
     *
75
     * @deprecated 2.0.0 Should not have been default behaviour. Will be removed as of the next major release.
76
     */
77 14
    private function addSuppressionMiddleware(array $middleware): array
78
    {
79 14
        foreach ($middleware as $instance) {
80 10
            if ($instance instanceof SuppressionMiddleware) {
81 10
                return $middleware;
82
            }
83
        }
84
85 14
        $middleware[] = new SuppressionMiddleware();
86
87 14
        return $middleware;
88
    }
89
90
    /**
91
     * @param MiddlewareInterface[] $middleware
92
     *
93
     * @return callable
94
     */
95 14
    private function chainMiddleware(array $middleware): callable
96
    {
97 14
        $middleware = array_reverse($middleware);
98
99
        return array_reduce($middleware, function (callable $next, MiddlewareInterface $middleware) {
100 14
            return function (View $view) use ($middleware, $next): View {
101 10
                return $middleware->process($view, $this->request, $next);
102 14
            };
103
        }, function (View $view): View {
104 7
            $view->render();
105
106 4
            return $view;
107 14
        });
108
    }
109
}
110