Completed
Push — middleware-wip ( 1f2854...9da264 )
by Romain
03:01
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\Core\Core;
17
use Romm\Formz\Form\Definition\Step\Step\Step;
18
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
19
use Romm\Formz\Form\FormObject\FormObject;
20
use Romm\Formz\Form\FormObject\Service\Step\FormStepPersistence;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Extbase\Mvc\Web\Request;
23
24
class FormObjectSteps
25
{
26
    const METADATA_STEP_PERSISTENCE_KEY = 'core.formStepPersistence';
27
28
    /**
29
     * @var FormObject
30
     */
31
    protected $formObject;
32
33
    /**
34
     * Step persistence is saved in the form metadata.
35
     *
36
     * It allows having essential information about the form steps whenever it
37
     * is needed: submitted form values, as well as steps that were already
38
     * validated.
39
     *
40
     * @var FormStepPersistence
41
     */
42
    protected $stepPersistence;
43
44
    /**
45
     * @var Step
46
     */
47
    protected $currentStep;
48
49
    /**
50
     * @var SubstepDefinition
51
     */
52
    protected $currentSubstepDefinition;
53
54
    /**
55
     * @param FormObject $formObject
56
     */
57
    public function __construct(FormObject $formObject)
58
    {
59
        $this->formObject = $formObject;
60
    }
61
62
    /**
63
     * This function will search among the registered steps to find the one that
64
     * has the same controller parameters.
65
     *
66
     * It is also possible not to find any step, in this case `null` is
67
     * returned.
68
     *
69
     * @todo: memoization with request spl object storage
70
     *
71
     * @param Request $request
72
     */
73
    public function fetchCurrentStep(Request $request)
74
    {
75
        if (null !== $this->currentStep) {
76
            return;
77
        }
78
79
        $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...
80
81
        $configuration = $this->formObject->getDefinition();
82
83
        if ($configuration->hasSteps()) {
84
            foreach ($configuration->getSteps()->getEntries() as $step) {
85
                $data = [
86
                    // @todo: no page uid to fetch?
87
//                $step->getPageUid()    => Core::get()->getPageController()->id,
88
                    $step->getExtension()  => $request->getControllerExtensionName(),
89
                    $step->getController() => $request->getControllerName(),
90
                    $step->getAction()     => $request->getControllerActionName(),
91
                ];
92
93
                foreach ($data as $stepData => $requestData) {
94
                    if (false === empty($stepData)
95
                        && $stepData !== $requestData
96
                    ) {
97
                        continue 2;
98
                    }
99
                }
100
101
                if ($this->currentStep instanceof Step) {
102
                    throw new \Exception('todo'); // @todo
103
                }
104
105
                $this->currentStep = $step;
106
            }
107
        }
108
    }
109
110
    /**
111
     * @return Step|null
112
     */
113
    public function getCurrentStep()
114
    {
115
        if (null === $this->currentStep) {
116
            throw new \Exception('todo'); // @todo
117
        }
118
119
        return $this->currentStep ?: null;
120
    }
121
122
    /**
123
     * Fetches the step persistence object for the form, which may have been
124
     * stored in the form metadata.
125
     *
126
     * If the form object hash did change since the persistence object was saved
127
     * it is "refreshed" with the new hash (some data are also deleted as they
128
     * are no longer considered as valid).
129
     *
130
     * @return FormStepPersistence
131
     */
132
    public function getStepPersistence()
133
    {
134
        // @todo check configuration has steps or fatal error
135
        if (null === $this->stepPersistence) {
136
            $objectHash = $this->formObject->getObjectHash();
137
            $metadata = $this->formObject->getFormMetadata()->getMetadata();
138
139
            if ($metadata->has(self::METADATA_STEP_PERSISTENCE_KEY)) {
140
                $this->stepPersistence = $metadata->get(self::METADATA_STEP_PERSISTENCE_KEY);
141
142
                if (false === $this->stepPersistence instanceof FormStepPersistence) {
143
                    unset($this->stepPersistence);
144
                } elseif ($objectHash !== $this->stepPersistence->getObjectHash()) {
145
                    $this->stepPersistence->refreshObjectHash($objectHash);
146
                }
147
            }
148
149
            if (null === $this->stepPersistence) {
150
                $this->stepPersistence = GeneralUtility::makeInstance(FormStepPersistence::class, $objectHash);
151
                $metadata->set(self::METADATA_STEP_PERSISTENCE_KEY, $this->stepPersistence);
152
            }
153
        }
154
155
        return $this->stepPersistence;
156
    }
157
158
    /**
159
     * @return SubstepDefinition
160
     */
161
    public function getCurrentSubstepDefinition()
162
    {
163
        // @todo check current step has been set?
164
165
        if (null === $this->currentSubstepDefinition) {
166
            $this->currentSubstepDefinition = $this->getCurrentStep()->getSubsteps()->getFirstSubstepDefinition();
167
        }
168
169
        return $this->currentSubstepDefinition;
170
    }
171
172
    /**
173
     * @param SubstepDefinition $currentSubstepDefinition
174
     */
175
    public function setCurrentSubstepDefinition(SubstepDefinition $currentSubstepDefinition)
176
    {
177
        // @todo check current step has been set?
178
        $this->currentSubstepDefinition = $currentSubstepDefinition;
179
    }
180
}
181