Completed
Push — middleware-wip-tmp2 ( 51115e...a3478c )
by Romain
02:39
created

StepMiddlewareService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 11
dl 0
loc 104
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 4 1
B validateStep() 0 56 7
A getStepValidationResult() 0 19 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\Middleware\Item\Step\Service;
15
16
use Romm\Formz\Configuration\Form\Step\Step\Step;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Error\FormResult;
19
use Romm\Formz\Form\FormObject\FormObject;
20
use Romm\Formz\Middleware\Item\FormValidation\FormValidationMiddlewareOption;
21
use Romm\Formz\Service\Traits\SelfInstantiateTrait;
22
use Romm\Formz\Validation\Validator\Form\AbstractFormValidator;
23
use TYPO3\CMS\Core\SingletonInterface;
24
25
class StepMiddlewareService implements SingletonInterface
26
{
27
    use SelfInstantiateTrait;
28
29
    const METADATA_STEP_KEY = 'core.step';
30
31
    /**
32
     * @var FormObject
33
     */
34
    protected $formObject;
35
36
    /**
37
     * @param FormObject $formObject
38
     */
39
    public function reset(FormObject $formObject)
40
    {
41
        $this->formObject = $formObject;
42
    }
43
44
    /**
45
     * @param Step $step
46
     * @return Step|null
47
     */
48
    public function validateStep(Step $step)
49
    {
50
        /*
51
         * The first step is always valid.
52
         */
53
        $firstStep = $this->formObject->getConfiguration()->getSteps()->getFirstStep();
54
55
        if ($step === $firstStep) {
56
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(111, '111');
57
            return null;
58
        }
59
60
        /*
61
         * If there is no form instance, and the request is not in the first
62
         * step, obviously the user should not be there.
63
         */
64
        if (false === $this->formObject->hasForm()) {
65
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(222, '222');
66
            return $firstStep;
67
        }
68
69
        /*
70
         * @todo
71
         */
72
        $stepsToTest = [];
73
        $stepToRedirect = $firstStep;
74
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($step, '$step??');
75
76
        while ($step->hasPreviousStep()) {
77
            $step = $step->getPreviousStep();
78
            $stepsToTest[] = $step;
79
        }
80
81
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($stepsToTest, '$stepsToTest');
82
83
        foreach ($stepsToTest as $step) {
84
            $result = $this->getStepValidationResult($step);
85
86
            if ($result->hasErrors()) {
87
                $stepToRedirect = $step;
88
            }
89
        }
90
91
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($stepToRedirect, '$lastStepWithErrors 333');
92
        return $stepToRedirect;
93
94
95
        $key = 'core.step';
0 ignored issues
show
Unused Code introduced by
$key = 'core.step'; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
96
        $metadata = $this->formObject->getFormMetadata()->getMetadata();
97
98
        if ($metadata->has(self::METADATA_STEP_KEY)) {
99
            // @todo
100
        } else {
101
            $metadata->set($key, 1);
102
        }
103
    }
104
105
    /**
106
     * @param Step $step
107
     * @return FormResult
108
     */
109
    protected function getStepValidationResult(Step $step)
110
    {
111
        /** @var FormValidationMiddlewareOption $formValidationMiddlewareOptions */
112
        $formValidationMiddlewareOptions = $this->formObject
113
            ->getConfiguration()
114
            ->getPresetMiddlewares()
115
            ->getFormValidationMiddleware()
116
            ->getOptions();
117
118
        /** @var AbstractFormValidator $validator */
119
        $validator = Core::instantiate(
120
            $formValidationMiddlewareOptions->getFormValidatorClassName(),
121
            ['name' => $this->formObject->getName()]
122
        );
123
124
        $validator->getDataObject()->setValidatedStep($step);
125
126
        return $validator->validate($this->formObject->getForm());
127
    }
128
}
129