Completed
Push — middleware-wip ( fd2413...a8f94d )
by Romain
22:23
created

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