Completed
Push — middleware-wip ( f76eb7...e5756f )
by Romain
07:43
created

FormObject::getCurrentSubstep()   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\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->proxy = $this->createProxy($form);
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function formWasSubmitted()
136
    {
137
        return $this->hasForm() && $this->getProxy()->formWasSubmitted();
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function formWasValidated()
144
    {
145
        return $this->hasForm() && $this->getProxy()->formWasValidated();
146
    }
147
148
    /**
149
     * @return FormResult
150
     */
151
    public function getFormResult()
152
    {
153
        return $this->getProxy()->getFormResult();
154
    }
155
156
    /**
157
     * @return FormObjectRequestData
158
     */
159
    public function getRequestData()
160
    {
161
        return $this->getProxy()->getRequestData();
162
    }
163
    /**
164
     * @return FormMetadata
165
     */
166
    public function getFormMetadata()
167
    {
168
        return $this->getProxy()->getFormMetadata();
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getFormHash()
175
    {
176
        return $this->getProxy()->getFormHash();
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getObjectHash()
183
    {
184
        return $this->static->getObjectHash();
185
    }
186
187
    /**
188
     * @return PersistenceManager
189
     */
190
    public function getPersistenceManager()
191
    {
192
        return $this->persistenceManager;
193
    }
194
195
    /**
196
     * @return bool
197
     */
198
    public function hasSteps()
199
    {
200
        return $this->getDefinition()->hasSteps();
201
    }
202
203
    /**
204
     * @param Request $request
205
     * @return Step|null
206
     */
207
    public function getCurrentStep(Request $request)
208
    {
209
        return $this->getProxy()->getCurrentStep($request);
210
    }
211
212
    /**
213
     * @return Substep|null
214
     */
215
    public function getCurrentSubstep()
216
    {
217
        return $this->getProxy()->getCurrentSubstep();
218
219
    }
220
221
    /**
222
     * @return FormObjectProxy
223
     * @throws PropertyNotAccessibleException
224
     */
225
    protected function getProxy()
226
    {
227
        if (null === $this->proxy) {
228
            throw PropertyNotAccessibleException::formInstanceNotSet();
229
        }
230
231
        return $this->proxy;
232
    }
233
234
    /**
235
     * Wrapper for unit tests.
236
     *
237
     * @param FormInterface $form
238
     * @return FormObjectProxy
239
     */
240
    protected function createProxy(FormInterface $form)
241
    {
242
        return FormObjectFactory::get()->getProxy($form);
243
    }
244
}
245