Completed
Push — feature/middleware ( 864c18 )
by Romain
03:21
created

FormObjectProxy   B

Complexity

Total Complexity 20

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 7
cbo 4
dl 0
loc 201
rs 8.3333
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getForm() 0 4 1
A markFormAsSubmitted() 0 4 1
A formWasSubmitted() 0 4 1
A markFormAsValidated() 0 4 1
A formWasValidated() 0 4 1
A getFormResult() 0 8 2
A getRequestData() 0 4 1
A getFormMetadata() 0 8 2
A getCurrentStep() 0 4 1
A fetchCurrentStep() 0 4 1
A getCurrentSubstepDefinition() 0 4 1
A setCurrentSubstepDefinition() 0 4 1
A getStepPersistence() 0 4 1
A getFormHash() 0 8 2
A setFormHash() 0 4 1
A injectRequestData() 0 4 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\Core\Core;
17
use Romm\Formz\Domain\Model\FormMetadata;
18
use Romm\Formz\Error\FormResult;
19
use Romm\Formz\Form\Definition\Step\Step\Step;
20
use Romm\Formz\Form\Definition\Step\Step\Substep\Substep;
21
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
22
use Romm\Formz\Form\FormInterface;
23
use Romm\Formz\Form\FormObject\Service\FormObjectMetadata;
24
use Romm\Formz\Form\FormObject\Service\FormObjectRequestData;
25
use Romm\Formz\Form\FormObject\Service\FormObjectSteps;
26
use Romm\Formz\Form\FormObject\Service\Step\FormStepPersistence;
27
use Romm\Formz\Service\HashService;
28
use TYPO3\CMS\Extbase\Mvc\Web\Request;
29
30
class FormObjectProxy
31
{
32
    /**
33
     * @var FormObject
34
     */
35
    protected $formObject;
36
37
    /**
38
     * @var FormInterface
39
     */
40
    protected $form;
41
42
    /**
43
     * @var string
44
     */
45
    protected $formHash;
46
47
    /**
48
     * @var bool
49
     */
50
    protected $formWasSubmitted = false;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $formWasValidated = false;
56
57
    /**
58
     * @var FormResult
59
     */
60
    protected $formResult;
61
62
    /**
63
     * @var FormObjectRequestData
64
     */
65
    protected $requestData;
66
67
    /**
68
     * @var FormObjectMetadata
69
     */
70
    protected $metadata;
71
72
    /**
73
     * @var FormObjectSteps
74
     */
75
    protected $stepService;
76
77
    /**
78
     * @param FormObject    $formObject
79
     * @param FormInterface $form
80
     */
81
    public function __construct(FormObject $formObject, FormInterface $form)
82
    {
83
        $this->formObject = $formObject;
84
        $this->form = $form;
85
        $this->stepService = Core::instantiate(FormObjectSteps::class, $formObject);
86
    }
87
88
    /**
89
     * @return FormInterface
90
     */
91
    public function getForm()
92
    {
93
        return $this->form;
94
    }
95
96
    /**
97
     * Will mark the form as submitted (change the result returned by the
98
     * function `formWasSubmitted()`).
99
     */
100
    public function markFormAsSubmitted()
101
    {
102
        $this->formWasSubmitted = true;
103
    }
104
105
    /**
106
     * Returns `true` if the form was submitted by the user.
107
     *
108
     * @return bool
109
     */
110
    public function formWasSubmitted()
111
    {
112
        return $this->formWasSubmitted;
113
    }
114
115
    /**
116
     * Marks the form as validated.
117
     */
118
    public function markFormAsValidated()
119
    {
120
        $this->formWasValidated = true;
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function formWasValidated()
127
    {
128
        return $this->formWasValidated;
129
    }
130
131
    /**
132
     * @return FormResult
133
     */
134
    public function getFormResult()
135
    {
136
        if (null === $this->formResult) {
137
            $this->formResult = new FormResult;
138
        }
139
140
        return $this->formResult;
141
    }
142
143
    /**
144
     * @return FormObjectRequestData
145
     */
146
    public function getRequestData()
147
    {
148
        return $this->requestData;
149
    }
150
151
    /**
152
     * @return FormMetadata
153
     */
154
    public function getFormMetadata()
155
    {
156
        if (null === $this->metadata) {
157
            $this->metadata = Core::instantiate(FormObjectMetadata::class, $this->formObject);
158
        }
159
160
        return $this->metadata->getMetadata();
161
    }
162
163
    /**
164
     * @return Step|null
165
     */
166
    public function getCurrentStep()
167
    {
168
        return $this->stepService->getCurrentStep();
169
    }
170
171
    /**
172
     * @param Request $request
173
     */
174
    public function fetchCurrentStep(Request $request)
175
    {
176
        $this->stepService->fetchCurrentStep($request);
177
    }
178
179
    /**
180
     * @return SubstepDefinition
181
     */
182
    public function getCurrentSubstepDefinition()
183
    {
184
        return $this->stepService->getCurrentSubstepDefinition();
185
    }
186
187
    /**
188
     * @param SubstepDefinition $currentSubstepDefinition
189
     */
190
    public function setCurrentSubstepDefinition(SubstepDefinition $currentSubstepDefinition)
191
    {
192
        $this->stepService->setCurrentSubstepDefinition($currentSubstepDefinition);
193
    }
194
195
    /**
196
     * @return FormStepPersistence
197
     */
198
    public function getStepPersistence()
199
    {
200
        return $this->stepService->getStepPersistence();
201
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getFormHash()
207
    {
208
        if (null === $this->formHash) {
209
            $this->formHash = HashService::get()->getHash(uniqid(get_class($this->form)));
210
        }
211
212
        return $this->formHash;
213
    }
214
215
    /**
216
     * @param string $hash
217
     */
218
    public function setFormHash($hash)
219
    {
220
        $this->formHash = $hash;
221
    }
222
223
    /**
224
     * @param FormObjectRequestData $requestData
225
     */
226
    public function injectRequestData(FormObjectRequestData $requestData)
227
    {
228
        $this->requestData = $requestData;
229
    }
230
}
231