Completed
Push — middleware-wip ( 75f70c...769ffe )
by Romain
03:08
created

FormObjectSteps::getStepPersistence()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 9
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
     * @param FormObject $formObject
55
     */
56
    public function __construct(FormObject $formObject)
57
    {
58
        $this->formObject = $formObject;
59
    }
60
61
    /**
62
     * This function will search among the registered steps to find the one that
63
     * has the same controller parameters.
64
     *
65
     * It is also possible not to find any step, in this case `null` is
66
     * returned.
67
     *
68
     * @todo: memoization with request spl object storage
69
     *
70
     * @param Request $request
71
     */
72
    public function fetchCurrentStep(Request $request)
73
    {
74
        if (null !== $this->currentStep) {
75
            return;
76
        }
77
78
        $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...
79
80
        $configuration = $this->formObject->getDefinition();
81
82
        if ($configuration->hasSteps()) {
83
            foreach ($configuration->getSteps()->getEntries() as $step) {
84
                $data = [
85
                    // @todo: no page uid to fetch?
86
//                $step->getPageUid()    => Core::get()->getPageController()->id,
87
                    $step->getExtension()  => $request->getControllerExtensionName(),
88
                    $step->getController() => $request->getControllerName()
89
                ];
90
91
                foreach ($data as $stepData => $requestData) {
92
                    if (false === empty($stepData)
93
                        && $stepData !== $requestData
94
                    ) {
95
                        continue 2;
96
                    }
97
                }
98
99
                $actionList = GeneralUtility::trimExplode(',', $step->getAction());
100
101
                if (false === in_array($request->getControllerActionName(), $actionList)) {
102
                    continue;
103
                }
104
105
                if ($this->currentStep instanceof Step) {
106
                    throw new \Exception('todo'); // @todo
107
                }
108
109
                $this->currentStep = $step;
110
            }
111
        }
112
    }
113
114
    /**
115
     * @return Step|null
116
     */
117
    public function getCurrentStep()
118
    {
119
        if (null === $this->currentStep) {
120
            throw new \Exception('todo'); // @todo
121
        }
122
123
        return $this->currentStep ?: null;
124
    }
125
126
    /**
127
     * Fetches the step persistence object for the form, which may have been
128
     * stored in the form metadata.
129
     *
130
     * If the form object hash did change since the persistence object was saved
131
     * it is "refreshed" with the new hash (some data are also deleted as they
132
     * are no longer considered as valid).
133
     *
134
     * @return FormStepPersistence
135
     */
136
    public function getStepPersistence()
137
    {
138
        // @todo check configuration has steps or fatal error
139
        if (null === $this->stepPersistence) {
140
            $objectHash = $this->formObject->getObjectHash();
141
            $metadata = $this->formObject->getFormMetadata()->getMetadata();
142
143
            if ($metadata->has(self::METADATA_STEP_PERSISTENCE_KEY)) {
144
                $this->stepPersistence = $metadata->get(self::METADATA_STEP_PERSISTENCE_KEY);
145
146
                if (false === $this->stepPersistence instanceof FormStepPersistence) {
147
                    unset($this->stepPersistence);
148
                } elseif ($objectHash !== $this->stepPersistence->getObjectHash()) {
149
                    $this->stepPersistence->refreshObjectHash($objectHash);
150
                }
151
            }
152
153
            if (null === $this->stepPersistence) {
154
                $this->stepPersistence = GeneralUtility::makeInstance(FormStepPersistence::class, $objectHash);
155
                $metadata->set(self::METADATA_STEP_PERSISTENCE_KEY, $this->stepPersistence);
156
            }
157
        }
158
159
        return $this->stepPersistence;
160
    }
161
162
    /**
163
     * @return SubstepDefinition
164
     */
165
    public function getCurrentSubstepDefinition()
166
    {
167
        // @todo check current step has been set?
168
169
        if (null === $this->currentSubstepDefinition) {
170
            $currentStep = $this->getCurrentStep();
171
172
            $this->currentSubstepDefinition = $currentStep->hasSubsteps()
173
                ? $currentStep->getSubsteps()->getFirstSubstepDefinition()
174
                : false;
175
        }
176
177
        return $this->currentSubstepDefinition ?: null;
178
    }
179
180
    /**
181
     * @param SubstepDefinition $currentSubstepDefinition
182
     */
183
    public function setCurrentSubstepDefinition(SubstepDefinition $currentSubstepDefinition)
184
    {
185
        // @todo check current step has been set?
186
        $this->currentSubstepDefinition = $currentSubstepDefinition;
187
    }
188
}
189