Completed
Push — middleware-wip ( 9fd801...caa06b )
by Romain
03:05
created

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