Completed
Push — resource-url-generator ( 23f763...cfa198 )
by Sam
18:27 queued 08:34
created

RequestProcessor::postRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 10
loc 10
rs 9.4285
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 View Code Duplication
        foreach ($this->filters as $filter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        foreach ($this->filters as $filter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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