Completed
Push — middleware-wip-tmp2 ( 51115e...a3478c )
by Romain
02:39
created

FormObject::getCurrentStep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Step;
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\Form\FormObject\Service\FormObjectSteps;
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 FormObjectFactory
53
     */
54
    protected $formObjectFactory;
55
56
    /**
57
     * @var PersistenceManager
58
     */
59
    protected $persistenceManager;
60
61
    /**
62
     * @var FormObjectSteps
63
     */
64
    protected $stepService;
65
66
    /**
67
     * You should never create a new instance of this class directly, use the
68
     * `FormObjectFactory->getInstanceFromClassName()` function instead.
69
     *
70
     * @param string           $name
71
     * @param FormObjectStatic $static
72
     */
73
    public function __construct($name, FormObjectStatic $static)
74
    {
75
        $this->name = $name;
76
        $this->static = $static;
77
78
        $this->persistenceManager = Core::instantiate(PersistenceManager::class, $this);
79
        $this->stepService = Core::instantiate(FormObjectSteps::class, $this);
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getName()
86
    {
87
        return $this->name;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getClassName()
94
    {
95
        return $this->static->getClassName();
96
    }
97
98
    /**
99
     * @param string $name
100
     * @return bool
101
     */
102
    public function hasProperty($name)
103
    {
104
        return $this->static->hasProperty($name);
105
    }
106
107
    /**
108
     * @return Form
109
     */
110
    public function getConfiguration()
111
    {
112
        return $this->static->getConfiguration();
113
    }
114
115
    /**
116
     * @return Result
117
     */
118
    public function getConfigurationValidationResult()
119
    {
120
        return $this->static->getConfigurationValidationResult();
121
    }
122
123
    /**
124
     * @return FormInterface
125
     */
126
    public function getForm()
127
    {
128
        return $this->getProxy()->getForm();
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function hasForm()
135
    {
136
        return $this->proxy !== null;
137
    }
138
139
    /**
140
     * @param FormInterface $form
141
     * @throws DuplicateEntryException
142
     */
143
    public function setForm(FormInterface $form)
144
    {
145
        if ($this->proxy) {
146
            throw DuplicateEntryException::formInstanceAlreadyAdded($this);
147
        }
148
149
        $this->proxy = $this->createProxy($form);
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function formWasSubmitted()
156
    {
157
        return $this->hasForm() && $this->getProxy()->formWasSubmitted();
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function formWasValidated()
164
    {
165
        return $this->hasForm() && $this->getProxy()->formWasValidated();
166
    }
167
168
    /**
169
     * @return FormResult
170
     */
171
    public function getFormResult()
172
    {
173
        return $this->getProxy()->getFormResult();
174
    }
175
176
    /**
177
     * @return FormObjectRequestData
178
     */
179
    public function getRequestData()
180
    {
181
        return $this->getProxy()->getRequestData();
182
    }
183
184
    /**
185
     * @return FormMetadata
186
     */
187
    public function getFormMetadata()
188
    {
189
        return $this->getProxy()->getFormMetadata();
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getFormHash()
196
    {
197
        return $this->getProxy()->getFormHash();
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    public function getObjectHash()
204
    {
205
        return $this->static->getObjectHash();
206
    }
207
208
    /**
209
     * @return PersistenceManager
210
     */
211
    public function getPersistenceManager()
212
    {
213
        return $this->persistenceManager;
214
    }
215
216
    /**
217
     * This function will search among the registered steps to find the one that
218
     * has the same controller parameters.
219
     *
220
     * It is also possible not to find any step, in this case `null` is
221
     * returned.
222
     *
223
     * @param Request $request
224
     * @return Step|null
225
     */
226
    public function getCurrentStep(Request $request)
227
    {
228
        return $this->stepService->getCurrentStep($request);
229
    }
230
231
    /**
232
     * @return FormObjectProxy
233
     * @throws PropertyNotAccessibleException
234
     */
235
    protected function getProxy()
236
    {
237
        if (null === $this->proxy) {
238
            throw PropertyNotAccessibleException::formInstanceNotSet();
239
        }
240
241
        return $this->proxy;
242
    }
243
244
    /**
245
     * Wrapper for unit tests.
246
     *
247
     * @param FormInterface $form
248
     * @return FormObjectProxy
249
     */
250
    protected function createProxy(FormInterface $form)
251
    {
252
        return $this->formObjectFactory->getProxy($form);
253
    }
254
255
    /**
256
     * @param FormObjectFactory $formObjectFactory
257
     */
258
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
259
    {
260
        $this->formObjectFactory = $formObjectFactory;
261
    }
262
}
263