Completed
Push — wip/steps ( 116a8d...b2c91c )
by Romain
02:25
created

SubstepValidationService::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Validation\Form\Service;
15
16
use Romm\Formz\Condition\Processor\ConditionProcessorFactory;
17
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
18
use Romm\Formz\Error\FormResult;
19
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
20
use Romm\Formz\Form\FormObject\FormObject;
21
use Romm\Formz\Form\FormObject\FormObjectFactory;
22
use Romm\Formz\Validation\Form\DataObject\FormValidatorDataObject;
23
use Romm\Formz\Validation\Form\FormValidatorExecutor;
24
25
class SubstepValidationService
26
{
27
    /**
28
     * @var FormValidatorExecutor
29
     */
30
    protected $formValidatorExecutor;
31
32
    /**
33
     * @var FormValidatorDataObject
34
     */
35
    protected $dataObject;
36
37
    /**
38
     * @param FormValidatorExecutor   $formValidatorExecutor
39
     * @param FormValidatorDataObject $dataObject
40
     */
41
    public function __construct(FormValidatorExecutor $formValidatorExecutor, FormValidatorDataObject $dataObject)
42
    {
43
        $this->formValidatorExecutor = $formValidatorExecutor;
44
        $this->dataObject = $dataObject;
45
    }
46
47
    /**
48
     * @todo
49
     */
50
    public function handleSubsteps()
51
    {
52
        $stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
53
54
        $currentSubstepDefinition = $this->aze();
55
56
        if (null !== $currentSubstepDefinition
57
            && $this->dataObject->getValidatedStep() === $stepService->getCurrentStep()
58
        ) {
59
            if ($this->getResult()->hasErrors()) {
60
                $stepService->setCurrentSubstepDefinition($currentSubstepDefinition);
61
            } else {
62
                list($nextSubstep, $substepsLevelIncrease) = $this->getNextSubstep($currentSubstepDefinition);
63
64
                if ($nextSubstep) {
65
                    $stepService->setCurrentSubstepDefinition($nextSubstep);
66
                    $stepService->setSubstepsLevel($stepService->getSubstepsLevel() + $substepsLevelIncrease);
67
                } else {
68
                    $stepService->markLastSubstepAsValidated();
69
                }
70
            }
71
        }
72
    }
73
74
    protected function aze()
75
    {
76
        $stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
77
78
        $currentSubstepDefinition = null;
79
        $firstSubstepDefinition = $this->dataObject->getValidatedStep()->getSubsteps()->getFirstSubstepDefinition();
80
        $substepDefinition = $firstSubstepDefinition;
81
        $substepsLevel = $stepService->getSubstepsLevel();
82
        $stepService->setSubstepsLevel(1);
83
        $substepsLevelCounter = 0;
84
85
        while ($substepDefinition && $substepsLevel > 0) {
86
            $substepsLevel--;
87
            $substepsLevelCounter++;
88
            $phpResult = true;
89
90
            if ($substepDefinition->hasActivation()) {
91
                $phpResult = $this->getSubstepDefinitionActivationResult($substepDefinition);
92
            }
93
94
            if (true === $phpResult) {
95
                $supportedFields = $substepDefinition->getSubstep()->getSupportedFields();
96
97
                foreach ($supportedFields as $supportedField) {
98
                    $this->formValidatorExecutor->validateField($supportedField->getField());
99
                }
100
            }
101
102
            if ($substepsLevel === 0
103
                || $this->getResult()->hasErrors()
104
            ) {
105
                $currentSubstepDefinition = $substepDefinition;
106
                $stepService->setSubstepsLevel($substepsLevelCounter);
107
                break;
108
            }
109
110
            $substepDefinition = $substepDefinition->hasNextSubstep()
111
                ? $substepDefinition->getNextSubstep()
112
                : null;
113
        }
114
115
        return $currentSubstepDefinition;
116
    }
117
118
    protected function getNextSubstep(SubstepDefinition $substepDefinition)
119
    {
120
        $substepsLevelIncrease = 0;
121
        $nextSubstep = null;
122
123
        while ($substepDefinition) {
124
            if (false === $substepDefinition->hasNextSubstep()) {
125
                break;
126
            } else {
127
                $substepDefinition = $substepDefinition->getNextSubstep();
128
                $substepsLevelIncrease++;
129
130
                if (false === $substepDefinition->hasActivation()) {
131
                    $nextSubstep = $substepDefinition;
132
                    break;
133
                } else {
134
                    $phpResult = $this->getSubstepDefinitionActivationResult($substepDefinition);
135
136
                    if (true === $phpResult) {
137
                        $nextSubstep = $substepDefinition;
138
                        break;
139
                    }
140
                }
141
            }
142
        }
143
144
        return [$nextSubstep, $substepsLevelIncrease];
145
    }
146
147
    /**
148
     * @param SubstepDefinition $substepDefinition
149
     * @return bool
150
     */
151
    protected function getSubstepDefinitionActivationResult(SubstepDefinition $substepDefinition)
152
    {
153
        $conditionProcessor = ConditionProcessorFactory::getInstance()->get($this->getFormObject());
154
        $tree = $conditionProcessor->getActivationConditionTreeForSubstep($substepDefinition);
155
        $dataObject = new PhpConditionDataObject($this->getFormObject()->getForm(), $this->formValidatorExecutor);
156
157
        return $tree->getPhpResult($dataObject);
158
    }
159
160
    /**
161
     * @return FormObject
162
     */
163
    protected function getFormObject()
164
    {
165
        return $this->dataObject->getFormObject();
166
    }
167
168
    /**
169
     * @return FormResult
170
     */
171
    protected function getResult()
172
    {
173
        return $this->dataObject->getFormResult();
174
    }
175
}
176