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

FormObjectSteps::fetchCurrentStep()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 18
nc 8
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\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
                    $step->getAction()     => $request->getControllerActionName(),
90
                ];
91
92
                foreach ($data as $stepData => $requestData) {
93
                    if (false === empty($stepData)
94
                        && $stepData !== $requestData
95
                    ) {
96
                        continue 2;
97
                    }
98
                }
99
100
                if ($this->currentStep instanceof Step) {
101
                    throw new \Exception('todo'); // @todo
102
                }
103
104
                $this->currentStep = $step;
105
            }
106
        }
107
    }
108
109
    /**
110
     * @return Step|null
111
     */
112
    public function getCurrentStep()
113
    {
114
        if (null === $this->currentStep) {
115
            throw new \Exception('todo'); // @todo
116
        }
117
118
        return $this->currentStep ?: null;
119
    }
120
121
    /**
122
     * Fetches the step persistence object for the form, which may have been
123
     * stored in the form metadata.
124
     *
125
     * If the form object hash did change since the persistence object was saved
126
     * it is "refreshed" with the new hash (some data are also deleted as they
127
     * are no longer considered as valid).
128
     *
129
     * @return FormStepPersistence
130
     */
131
    public function getStepPersistence()
132
    {
133
        // @todo check configuration has steps or fatal error
134
        if (null === $this->stepPersistence) {
135
            $objectHash = $this->formObject->getObjectHash();
136
            $metadata = $this->formObject->getFormMetadata()->getMetadata();
137
138
            if ($metadata->has(self::METADATA_STEP_PERSISTENCE_KEY)) {
139
                $this->stepPersistence = $metadata->get(self::METADATA_STEP_PERSISTENCE_KEY);
140
141
                if (false === $this->stepPersistence instanceof FormStepPersistence) {
142
                    unset($this->stepPersistence);
143
                } elseif ($objectHash !== $this->stepPersistence->getObjectHash()) {
144
                    $this->stepPersistence->refreshObjectHash($objectHash);
145
                }
146
            }
147
148
            if (null === $this->stepPersistence) {
149
                $this->stepPersistence = GeneralUtility::makeInstance(FormStepPersistence::class, $objectHash);
150
                $metadata->set(self::METADATA_STEP_PERSISTENCE_KEY, $this->stepPersistence);
151
            }
152
        }
153
154
        return $this->stepPersistence;
155
    }
156
157
    /**
158
     * @return SubstepDefinition
159
     */
160
    public function getCurrentSubstepDefinition()
161
    {
162
        // @todo check current step has been set?
163
164
        if (null === $this->currentSubstepDefinition) {
165
            $currentStep = $this->getCurrentStep();
166
167
            $this->currentSubstepDefinition = $currentStep->hasSubsteps()
168
                ? $currentStep->getSubsteps()->getFirstSubstepDefinition()
169
                : false;
170
        }
171
172
        return $this->currentSubstepDefinition ?: null;
173
    }
174
175
    /**
176
     * @param SubstepDefinition $currentSubstepDefinition
177
     */
178
    public function setCurrentSubstepDefinition(SubstepDefinition $currentSubstepDefinition)
179
    {
180
        // @todo check current step has been set?
181
        $this->currentSubstepDefinition = $currentSubstepDefinition;
182
    }
183
}
184