Completed
Push — master ( daed8c...cf758d )
by Damian
08:03
created

RequestProcessor::process()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 14
nop 2
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Control;
4
5
use SilverStripe\Control\Middleware\HTTPMiddleware;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\Dev\Deprecation;
8
9
/**
10
 * Middleware that provides back-support for the deprecated RequestFilter API.
11
 *
12
 * @deprecated 4.0..5.0 Use HTTPMiddleware directly instead.
13
 */
14
class RequestProcessor implements HTTPMiddleware
15
{
16
    use Injectable;
17
18
    /**
19
     * List of currently assigned request filters
20
     *
21
     * @var RequestFilter[]
22
     */
23
    private $filters = array();
24
25
    /**
26
     * Construct new RequestFilter with a list of filter objects
27
     *
28
     * @param RequestFilter[] $filters
29
     */
30
    public function __construct($filters = array())
31
    {
32
        $this->filters = $filters;
33
    }
34
35
    /**
36
     * Assign a list of request filters
37
     *
38
     * @param RequestFilter[] $filters
39
     * @return $this
40
     */
41
    public function setFilters($filters)
42
    {
43
        $this->filters = $filters;
44
        return $this;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function process(HTTPRequest $request, callable $delegate)
51
    {
52
        if ($this->filters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->filters of type SilverStripe\Control\RequestFilter[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
            Deprecation::notice(
54
                '5.0',
55
                'Deprecated RequestFilters are in use. Apply HTTPMiddleware to Director.middlewares instead.'
56
            );
57
        }
58
59
        foreach ($this->filters as $filter) {
60
            $res = $filter->preRequest($request);
61
            if ($res === false) {
62
                return new HTTPResponse(_t(__CLASS__.'.INVALID_REQUEST', 'Invalid request'), 400);
63
            }
64
        }
65
66
        $response = $delegate($request);
67
68
        foreach ($this->filters as $filter) {
69
            $res = $filter->postRequest($request, $response);
70
            if ($res === false) {
71
                return new HTTPResponse(_t(__CLASS__ . '.REQUEST_ABORTED', 'Request aborted'), 500);
72
            }
73
        }
74
75
        return $response;
76
    }
77
}
78