Completed
Push — middleware-wip ( 17e892...bf36ad )
by Romain
02:41
created

AbstractMiddleware::getBoundSignalName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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\Core\Core;
19
use Romm\Formz\Error\FormResult;
20
use Romm\Formz\Exceptions\EntryNotFoundException;
21
use Romm\Formz\Form\FormObject;
22
use Romm\Formz\Middleware\Option\AbstractOptionDefinition;
23
use Romm\Formz\Middleware\Request\Exception\StopPropagationException;
24
use Romm\Formz\Middleware\MiddlewareInterface;
25
use Romm\Formz\Middleware\Request\Forward;
26
use Romm\Formz\Middleware\Request\Redirect;
27
use Romm\Formz\Middleware\Signal\MiddlewareSignal;
28
use Romm\Formz\Middleware\State\StateMiddlewareDependencyTrait;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
31
use TYPO3\CMS\Extbase\Mvc\Web\Request;
32
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
33
34
abstract class AbstractMiddleware implements MiddlewareInterface, DataPreProcessorInterface
35
{
36
    use StateMiddlewareDependencyTrait;
37
38
    /**
39
     * @var \Romm\Formz\Middleware\Option\DefaultOptionDefinition
40
     */
41
    protected $options;
42
43
    /**
44
     * @param AbstractOptionDefinition $options
45
     */
46
    public function __construct(AbstractOptionDefinition $options = null)
47
    {
48
        if (null === $options) {
49
            /** @var ReflectionService $reflectionService */
50
            $reflectionService = Core::instantiate(ReflectionService::class);
51
52
            $tag = $reflectionService->getPropertyTagValues(static::class, 'options', 'var');
53
54
            $options = GeneralUtility::makeInstance(ltrim(reset($tag), '\\'));
55
        }
56
57
        $this->options = $options;
58
    }
59
60
    /**
61
     * @todo
62
     */
63
    final public function initialize()
64
    {
65
        // @todo add options handling
66
67
        $this->initializeMiddleware();
68
    }
69
70
    /**
71
     * You can override this method in your child class to initialize your
72
     * middleware correctly.
73
     */
74
    protected function initializeMiddleware()
75
    {
76
    }
77
78
    /**
79
     * Will stop the propagation of middlewares: the next middlewares wont be
80
     * processed.
81
     *
82
     * Use with caution!
83
     */
84
    final protected function stopPropagation()
85
    {
86
        throw new StopPropagationException;
87
    }
88
89
    /**
90
     * Returns a new forward dispatcher, on which you can add options by calling
91
     * its fluent methods.
92
     *
93
     * You must call the method `dispatch()` to actually dispatch the forward
94
     * signal.
95
     *
96
     * @return Forward
97
     */
98
    final protected function forward()
99
    {
100
        return new Forward($this->getRequest());
101
    }
102
103
    /**
104
     * Returns a new redirect dispatcher, on which you can add options by
105
     * calling its fluent methods.
106
     *
107
     * You must call the method `dispatch()` to actually dispatch the redirect
108
     * signal.
109
     *
110
     * @return Redirect
111
     */
112
    final protected function redirect() {
113
        return new Redirect($this->getRequest());
114
    }
115
116
    /**
117
     * @return FormObject
118
     */
119
    final protected function getFormObject()
120
    {
121
        return $this->state->getFormObject();
122
    }
123
124
    /**
125
     * @return FormResult
126
     */
127
    final protected function getResult()
128
    {
129
        return $this->state->getResult();
130
    }
131
132
    /**
133
     * @return Request
134
     */
135
    final protected function getRequest()
136
    {
137
        return $this->state->getRequest();
138
    }
139
140
    /**
141
     * @return Arguments
142
     */
143
    final protected function getArguments()
144
    {
145
        return $this->state->getArguments();
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    final protected function getSettings()
152
    {
153
        return $this->state->getSettings();
154
    }
155
156
    /**
157
     * @return int
158
     */
159
    public function getPriority()
160
    {
161
        return 0;
162
    }
163
164
    /**
165
     * Returns the name of the signal on which this middleware is bound.
166
     *
167
     * @return string
168
     * @throws EntryNotFoundException
169
     */
170
    final public function getBoundSignalName()
171
    {
172
        $interfaces = class_implements($this);
173
174
        foreach ($interfaces as $interface) {
175
            if (in_array(MiddlewareSignal::class, class_implements($interface))) {
176
                return $interface;
177
            }
178
        }
179
180
        throw new EntryNotFoundException('@todo'); // @todo
181
    }
182
183
    /**
184
     * @todo
185
     *
186
     * @param DataPreProcessor $processor
187
     */
188
    public static function dataPreProcessor(DataPreProcessor $processor)
189
    {
190
        $data = $processor->getData();
191
192
        if (false === isset($data['options'])) {
193
            $data['options'] = [];
194
        }
195
196
        $processor->setData($data);
197
    }
198
}
199