Completed
Push — middleware-wip ( fd2413...a8f94d )
by Romain
22:23
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\Configuration\Form\Step\Step\Step;
17
use Romm\Formz\Core\Core;
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
     * @param FormObject $formObject
45
     */
46
    public function __construct(FormObject $formObject)
47
    {
48
        $this->formObject = $formObject;
49
    }
50
51
    /**
52
     * This function will search among the registered steps to find the one that
53
     * has the same controller parameters.
54
     *
55
     * It is also possible not to find any step, in this case `null` is
56
     * returned.
57
     *
58
     * @param Request $request
59
     * @return Step|null
60
     */
61
    public function getCurrentStep(Request $request)
62
    {
63
        $configuration = $this->formObject->getConfiguration();
64
65
        if (false === $configuration->hasSteps()) {
66
            return null;
67
        }
68
69
        $currentStep = null;
70
71
        foreach ($configuration->getSteps()->getEntries() as $step) {
72
            $data = [
73
                $step->getPageUid()    => Core::get()->getPageController()->id,
74
                $step->getExtension()  => $request->getControllerExtensionName(),
75
                $step->getController() => $request->getControllerName(),
76
                $step->getAction()     => $request->getControllerActionName(),
77
            ];
78
79
            foreach ($data as $stepData => $requestData) {
80
                if (false === empty($stepData)
81
                    && $stepData !== $requestData
82
                ) {
83
                    continue 2;
84
                }
85
            }
86
87
            if (null !== $currentStep) {
88
                throw new \Exception('todo'); // @todo
89
            }
90
91
            $currentStep = $step;
92
        }
93
94
        return $currentStep;
95
    }
96
97
    /**
98
     * Fetches the step persistence object for the form, which may have been
99
     * stored in the form metadata.
100
     *
101
     * If the form object hash did change since the persistence object was saved
102
     * it is "refreshed" with the new hash (some data are also deleted as they
103
     * are no longer considered as valid).
104
     *
105
     * @return FormStepPersistence
106
     */
107
    public function getStepPersistence()
108
    {
109
        // @todo check configuration has steps or fatal error
110
        if (null === $this->stepPersistence) {
111
            $objectHash = $this->formObject->getObjectHash();
112
            $metadata = $this->formObject->getFormMetadata()->getMetadata();
113
114
            if ($metadata->has(self::METADATA_STEP_PERSISTENCE_KEY)) {
115
                $this->stepPersistence = $metadata->get(self::METADATA_STEP_PERSISTENCE_KEY);
116
117
                if (false === $this->stepPersistence instanceof FormStepPersistence) {
118
                    unset($this->stepPersistence);
119
                } elseif ($objectHash !== $this->stepPersistence->getObjectHash()) {
120
                    $this->stepPersistence->refreshObjectHash($objectHash);
121
                }
122
            }
123
124
            if (null === $this->stepPersistence) {
125
                $this->stepPersistence = GeneralUtility::makeInstance(FormStepPersistence::class, $objectHash);
126
                $metadata->set(self::METADATA_STEP_PERSISTENCE_KEY, $this->stepPersistence);
127
            }
128
        }
129
130
        return $this->stepPersistence;
131
    }
132
}
133