Completed
Push — wip/steps ( f858ae...f1ed33 )
by Romain
10:54
created

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

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
244
    }
245
246
    /**
247
     * @param Request $request
248
     * @return $this
249
     */
250
    public function fetchCurrentStep(Request $request)
251
    {
252
        $this->stepService->fetchCurrentStep($request->getControllerExtensionName(), $request->getControllerName(), $request->getControllerActionName());
253
254
        return $this;
255
    }
256
257
    /**
258
     * @return SubstepDefinition
259
     */
260
    public function getCurrentSubstepDefinition()
261
    {
262
        return $this->stepService->getCurrentSubstepDefinition();
263
    }
264
265
    /**
266
     * @param FormInterface $form
267
     */
268
    protected function registerFormInstance(FormInterface $form)
269
    {
270
        if (false === FormObjectFactory::get()->formInstanceWasRegistered($form)) {
271
            FormObjectFactory::get()->registerFormInstance($form, $this->getName());
272
        }
273
    }
274
275
    /**
276
     * @return FormObjectProxy
277
     * @throws PropertyNotAccessibleException
278
     */
279
    protected function getProxy()
280
    {
281
        if (null === $this->proxy) {
282
            throw PropertyNotAccessibleException::formInstanceNotSet();
283
        }
284
285
        return $this->proxy;
286
    }
287
288
    /**
289
     * Wrapper for unit tests.
290
     *
291
     * @param FormInterface $form
292
     * @return FormObjectProxy
293
     */
294
    protected function createProxy(FormInterface $form)
295
    {
296
        return FormObjectFactory::get()->getProxy($form);
297
    }
298
}
299