Completed
Push — middleware-wip ( 844477...d1e0ab )
by Romain
02:47
created

AbstractMiddleware::getRequestArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Middleware\Item;
15
16
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
17
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
18
use Romm\Formz\Error\FormResult;
19
use Romm\Formz\Exceptions\EntryNotFoundException;
20
use Romm\Formz\Form\FormObject;
21
use Romm\Formz\Middleware\Option\AbstractOptionDefinition;
22
use Romm\Formz\Middleware\Request\Exception\StopPropagationException;
23
use Romm\Formz\Middleware\MiddlewareInterface;
24
use Romm\Formz\Middleware\Request\Forward;
25
use Romm\Formz\Middleware\Request\Redirect;
26
use Romm\Formz\Middleware\Signal\MiddlewareSignal;
27
use Romm\Formz\Middleware\State\StateMiddlewareDependencyTrait;
28
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
29
use TYPO3\CMS\Extbase\Mvc\Web\Request;
30
31
/**
32
 * Abstract class that must be extended by middlewares.
33
 *
34
 * Child middleware must implement their own signals.
35
 */
36
abstract class AbstractMiddleware implements MiddlewareInterface, DataPreProcessorInterface
37
{
38
    use StateMiddlewareDependencyTrait;
39
40
    /**
41
     * @var \Romm\Formz\Middleware\Option\DefaultOptionDefinition
42
     */
43
    protected $options;
44
45
    /**
46
     * @param AbstractOptionDefinition $options
47
     */
48
    final public function __construct(AbstractOptionDefinition $options)
49
    {
50
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
$options is of type object<Romm\Formz\Middle...stractOptionDefinition>, but the property $options was declared to be of type object<Romm\Formz\Middle...efaultOptionDefinition>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
51
    }
52
53
    /**
54
     * Abstraction for processing the middleware initialization.
55
     *
56
     * For own initialization, @see initializeMiddleware()
57
     */
58
    final public function initialize()
59
    {
60
        $this->initializeMiddleware();
61
    }
62
63
    /**
64
     * You can override this method in your child class to initialize your
65
     * middleware correctly.
66
     */
67
    protected function initializeMiddleware()
68
    {
69
    }
70
71
    /**
72
     * Returns a new forward dispatcher, on which you can add options by calling
73
     * its fluent methods.
74
     *
75
     * You must call the method `dispatch()` to actually dispatch the forward
76
     * signal.
77
     *
78
     * @return Forward
79
     */
80
    final protected function forward()
81
    {
82
        return new Forward($this->getRequest());
83
    }
84
85
    /**
86
     * Returns a new redirect dispatcher, on which you can add options by
87
     * calling its fluent methods.
88
     *
89
     * You must call the method `dispatch()` to actually dispatch the redirect
90
     * signal.
91
     *
92
     * @return Redirect
93
     */
94
    final protected function redirect() {
95
        return new Redirect($this->getRequest());
96
    }
97
98
    /**
99
     * Will stop the propagation of middlewares: the next middlewares wont be
100
     * processed.
101
     *
102
     * Use with caution!
103
     */
104
    final protected function stopPropagation()
105
    {
106
        throw new StopPropagationException;
107
    }
108
109
    /**
110
     * @return FormObject
111
     */
112
    final protected function getFormObject()
113
    {
114
        return $this->state->getFormObject();
115
    }
116
117
    /**
118
     * @return FormResult
119
     */
120
    final protected function getResult()
121
    {
122
        return $this->state->getResult();
123
    }
124
125
    /**
126
     * @return Request
127
     */
128
    final protected function getRequest()
129
    {
130
        return $this->state->getRequest();
131
    }
132
133
    /**
134
     * @return Arguments
135
     */
136
    final protected function getRequestArguments()
137
    {
138
        return $this->state->getRequestArguments();
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    final protected function getSettings()
145
    {
146
        return $this->state->getSettings();
147
    }
148
149
    /**
150
     * @return int
151
     */
152
    public function getPriority()
153
    {
154
        return 0;
155
    }
156
157
    /**
158
     * Returns the name of the signal on which this middleware is bound.
159
     *
160
     * @return string
161
     * @throws EntryNotFoundException
162
     */
163
    final public function getBoundSignalName()
164
    {
165
        $interfaces = class_implements($this);
166
167
        foreach ($interfaces as $interface) {
168
            if (in_array(MiddlewareSignal::class, class_implements($interface))) {
169
                return $interface;
170
            }
171
        }
172
173
        throw new EntryNotFoundException('@todo'); // @todo
174
    }
175
176
    /**
177
     * Will inject empty options if no option has been defined at all.
178
     *
179
     * @param DataPreProcessor $processor
180
     */
181
    public static function dataPreProcessor(DataPreProcessor $processor)
182
    {
183
        $data = $processor->getData();
184
185
        if (false === isset($data['options'])) {
186
            $data['options'] = [];
187
        }
188
189
        $processor->setData($data);
190
    }
191
}
192