Completed
Push — middleware-wip ( 1f2854...9da264 )
by Romain
03:01
created

FormObject::getCurrentSubstepDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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