Completed
Push — middleware-wip ( aa44a7...fcef97 )
by Romain
03:00
created

FormObject::formWasValidated()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
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\Form\FormObject;
15
16
use Romm\Formz\Configuration\Form\Form;
17
use Romm\Formz\Configuration\Form\Step\Step\StepItem;
18
use Romm\Formz\Core\Core;
19
use Romm\Formz\Domain\Model\FormMetadata;
20
use Romm\Formz\Error\FormResult;
21
use Romm\Formz\Exceptions\DuplicateEntryException;
22
use Romm\Formz\Exceptions\PropertyNotAccessibleException;
23
use Romm\Formz\Form\FormInterface;
24
use Romm\Formz\Form\FormObject\Service\FormObjectRequestData;
25
use Romm\Formz\Persistence\PersistenceManager;
26
use TYPO3\CMS\Extbase\Error\Result;
27
use TYPO3\CMS\Extbase\Mvc\Web\Request;
28
29
/**
30
 * This is the object representation of a form. In here we can manage which
31
 * properties the form does have, its configuration, and more.
32
 */
33
class FormObject
34
{
35
    /**
36
     * @var string
37
     */
38
    protected $name;
39
40
    /**
41
     * @var FormObjectStatic
42
     */
43
    protected $static;
44
45
    /**
46
     * @var FormObjectProxy
47
     */
48
    protected $proxy;
49
50
    /**
51
     * @var FormObjectFactory
52
     */
53
    protected $formObjectFactory;
54
55
    /**
56
     * @var PersistenceManager
57
     */
58
    protected $persistenceManager;
59
60
    /**
61
     * You should never create a new instance of this class directly, use the
62
     * `FormObjectFactory->getInstanceFromClassName()` function instead.
63
     *
64
     * @param string           $name
65
     * @param FormObjectStatic $static
66
     */
67
    public function __construct($name, FormObjectStatic $static)
68
    {
69
        $this->name = $name;
70
        $this->static = $static;
71
72
        $this->persistenceManager = Core::instantiate(PersistenceManager::class, $this);
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getName()
79
    {
80
        return $this->name;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getClassName()
87
    {
88
        return $this->static->getClassName();
89
    }
90
91
    /**
92
     * @param string $name
93
     * @return bool
94
     */
95
    public function hasProperty($name)
96
    {
97
        return $this->static->hasProperty($name);
98
    }
99
100
    /**
101
     * @return Form
102
     */
103
    public function getConfiguration()
104
    {
105
        return $this->static->getConfiguration();
106
    }
107
108
    /**
109
     * @return Result
110
     */
111
    public function getConfigurationValidationResult()
112
    {
113
        return $this->static->getConfigurationValidationResult();
114
    }
115
116
    /**
117
     * @return FormInterface
118
     */
119
    public function getForm()
120
    {
121
        return $this->getProxy()->getForm();
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function hasForm()
128
    {
129
        return $this->proxy !== null;
130
    }
131
132
    /**
133
     * @param FormInterface $form
134
     * @throws DuplicateEntryException
135
     */
136
    public function setForm(FormInterface $form)
137
    {
138
        if ($this->proxy) {
139
            throw DuplicateEntryException::formInstanceAlreadyAdded($this);
140
        }
141
142
        $this->proxy = $this->createProxy($form);
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function formWasSubmitted()
149
    {
150
        return $this->hasForm() && $this->getProxy()->formWasSubmitted();
151
    }
152
153
    /**
154
     * @return bool
155
     */
156
    public function formWasValidated()
157
    {
158
        return $this->hasForm() && $this->getProxy()->formWasValidated();
159
    }
160
161
    /**
162
     * @return FormResult
163
     */
164
    public function getFormResult()
165
    {
166
        return $this->getProxy()->getFormResult();
167
    }
168
169
    /**
170
     * @return FormObjectRequestData
171
     */
172
    public function getRequestData()
173
    {
174
        return $this->getProxy()->getRequestData();
175
    }
176
177
    /**
178
     * @return FormMetadata
179
     */
180
    public function getFormMetadata()
181
    {
182
        return $this->getProxy()->getFormMetadata();
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getFormHash()
189
    {
190
        return $this->getProxy()->getFormHash();
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    public function getObjectHash()
197
    {
198
        return $this->static->getObjectHash();
199
    }
200
201
    /**
202
     * @return PersistenceManager
203
     */
204
    public function getPersistenceManager()
205
    {
206
        return $this->persistenceManager;
207
    }
208
209
    /**
210
     * This function will search among the registered steps to find the one that
211
     * has the same controller parameters.
212
     *
213
     * It is also possible not to find any step, in this case `null` is
214
     * returned.
215
     *
216
     * @param Request $request
217
     * @return StepItem|null
218
     */
219
    public function getCurrentStep(Request $request)
220
    {
221
        $configuration = $this->getConfiguration();
222
223
        if (false === $configuration->getStep()->hasSteps()) {
224
            return null;
225
        }
226
227
        $currentStep = null;
228
        $steps = $configuration->getStep()->getSteps();
229
230
        foreach ($steps as $step) {
231
            $data = [
232
                $step->getPageUid()    => Core::get()->getPageController()->id,
233
                $step->getExtension()  => $request->getControllerExtensionName(),
234
                $step->getController() => $request->getControllerName(),
235
                $step->getAction()     => $request->getControllerActionName(),
236
            ];
237
238
            foreach ($data as $stepData => $requestData) {
239
                if (false === empty($stepData)
240
                    && $stepData !== $requestData
241
                ) {
242
                    continue 2;
243
                }
244
            }
245
246
            if (null !== $currentStep) {
247
                throw new \Exception('todo'); // @todo
248
            }
249
250
            $currentStep = $step;
251
        }
252
253
        return $currentStep;
254
    }
255
256
    /**
257
     * @return FormObjectProxy
258
     * @throws PropertyNotAccessibleException
259
     */
260
    protected function getProxy()
261
    {
262
        if (null === $this->proxy) {
263
            throw PropertyNotAccessibleException::formInstanceNotSet();
264
        }
265
266
        return $this->proxy;
267
    }
268
269
    /**
270
     * Wrapper for unit tests.
271
     *
272
     * @param FormInterface $form
273
     * @return FormObjectProxy
274
     */
275
    protected function createProxy(FormInterface $form)
276
    {
277
        return $this->formObjectFactory->getProxy($this, $form);
278
    }
279
280
    /**
281
     * @param FormObjectFactory $formObjectFactory
282
     */
283
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
284
    {
285
        $this->formObjectFactory = $formObjectFactory;
286
    }
287
}
288