Completed
Push — wip/steps ( 0e4101...4c32fc )
by Romain
03:57
created

FormObjectSteps::getSubstepsLevel()   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\Service;
15
16
use Romm\Formz\Form\Definition\Step\Step\Step;
17
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
18
use Romm\Formz\Form\FormObject\FormObject;
19
use Romm\Formz\Form\FormObject\Service\Step\FormStepPersistence;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Mvc\Web\Request;
22
23
class FormObjectSteps
24
{
25
    const METADATA_STEP_PERSISTENCE_KEY = 'core.formStepPersistence';
26
27
    /**
28
     * @var FormObject
29
     */
30
    protected $formObject;
31
32
    /**
33
     * Step persistence is saved in the form metadata.
34
     *
35
     * It allows having essential information about the form steps whenever it
36
     * is needed: submitted form values, as well as steps that were already
37
     * validated.
38
     *
39
     * @var FormStepPersistence
40
     */
41
    protected $stepPersistence;
42
43
    /**
44
     * @var Step
45
     */
46
    protected $currentStep;
47
48
    /**
49
     * @var SubstepDefinition
50
     */
51
    protected $currentSubstepDefinition;
52
53
    /**
54
     * @var int
55
     */
56
    protected $substepsLevel = 1;
57
58
    /**
59
     * @var bool
60
     */
61
    protected $lastSubstepValidated = false;
62
63
    /**
64
     * @param FormObject $formObject
65
     */
66
    public function __construct(FormObject $formObject)
67
    {
68
        $this->formObject = $formObject;
69
    }
70
71
    /**
72
     * This function will search among the registered steps to find the one that
73
     * has the same controller parameters.
74
     *
75
     * It is also possible not to find any step, in this case `null` is
76
     * returned.
77
     *
78
     * @todo: memoization with request spl object storage
79
     *
80
     * @param Request $request
81
     */
82
    public function fetchCurrentStep(Request $request)
83
    {
84
        if (null !== $this->currentStep) {
85
            return;
86
        }
87
88
        $this->currentStep = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<Romm\Formz\Form\Definition\Step\Step\Step> of property $currentStep.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
90
        $configuration = $this->formObject->getDefinition();
91
92
        if ($configuration->hasSteps()) {
93
            foreach ($configuration->getSteps()->getEntries() as $step) {
94
                $data = [
95
                    // @todo: no page uid to fetch?
96
//                $step->getPageUid()    => Core::get()->getPageController()->id,
97
                    $step->getExtension() => $request->getControllerExtensionName(),
98
                    $step->getController() => $request->getControllerName()
99
                ];
100
101
                foreach ($data as $stepData => $requestData) {
102
                    if (false === empty($stepData)
103
                        && $stepData !== $requestData
104
                    ) {
105
                        continue 2;
106
                    }
107
                }
108
109
                $actionList = $step->getAuthorizedActions();
110
111
                if (false === in_array($request->getControllerActionName(), $actionList)) {
112
                    continue;
113
                }
114
115
                if ($this->currentStep instanceof Step) {
116
                    throw new \Exception('todo'); // @todo
117
                }
118
119
                $this->currentStep = $step;
120
            }
121
        }
122
    }
123
124
    /**
125
     * @param Step $step
126
     */
127
    public function setCurrentStep(Step $step)
128
    {
129
        $this->currentStep = $step;
130
    }
131
132
    /**
133
     * @return Step|null
134
     */
135
    public function getCurrentStep()
136
    {
137
        if (null === $this->currentStep) {
138
            throw new \Exception('todo'); // @todo
139
        }
140
141
        return $this->currentStep ?: null;
142
    }
143
144
    /**
145
     * Fetches the step persistence object for the form, which may have been
146
     * stored in the form metadata.
147
     *
148
     * If the form object hash did change since the persistence object was saved
149
     * it is "refreshed" with the new hash (some data are also deleted as they
150
     * are no longer considered as valid).
151
     *
152
     * @return FormStepPersistence
153
     */
154
    public function getStepPersistence()
155
    {
156
        // @todo check configuration has steps or fatal error
157
        if (null === $this->stepPersistence) {
158
            $objectHash = $this->formObject->getObjectHash();
159
            $metadata = $this->formObject->getFormMetadata();
160
161
            if ($metadata->has(self::METADATA_STEP_PERSISTENCE_KEY)) {
162
                $this->stepPersistence = $metadata->get(self::METADATA_STEP_PERSISTENCE_KEY);
163
164
                if (false === $this->stepPersistence instanceof FormStepPersistence) {
165
                    unset($this->stepPersistence);
166
                } elseif ($objectHash !== $this->stepPersistence->getObjectHash()) {
167
                    $this->stepPersistence->refreshObjectHash($objectHash);
168
                }
169
            }
170
171
            if (null === $this->stepPersistence) {
172
                $this->stepPersistence = GeneralUtility::makeInstance(FormStepPersistence::class, $objectHash);
173
                $metadata->set(self::METADATA_STEP_PERSISTENCE_KEY, $this->stepPersistence);
174
            }
175
        }
176
177
        return $this->stepPersistence;
178
    }
179
180
    /**
181
     * @return SubstepDefinition
182
     */
183
    public function getCurrentSubstepDefinition()
184
    {
185
        return $this->currentSubstepDefinition ?: $this->getCurrentStep()->getSubsteps()->getFirstSubstepDefinition();
186
    }
187
188
    /**
189
     * @param SubstepDefinition $currentSubstepDefinition
190
     */
191
    public function setCurrentSubstepDefinition(SubstepDefinition $currentSubstepDefinition)
192
    {
193
        $this->currentSubstepDefinition = $currentSubstepDefinition;
194
    }
195
196
    /**
197
     * @param int $level
198
     */
199
    public function setSubstepsLevel($level)
200
    {
201
        $this->substepsLevel = max(1, (int)$level);
202
    }
203
204
    /**
205
     * @return int
206
     */
207
    public function getSubstepsLevel()
208
    {
209
        return $this->substepsLevel;
210
    }
211
212
    /**
213
     * @todo
214
     */
215
    public function markLastSubstepAsValidated()
216
    {
217
        $this->lastSubstepValidated = true;
218
    }
219
220
    /**
221
     * @return bool
222
     */
223
    public function lastSubstepWasValidated()
224
    {
225
        return $this->lastSubstepValidated;
226
    }
227
}
228