Completed
Push — wip/steps ( b2c91c...6fd1d1 )
by Romain
02:28
created

getCurrentSubstepDefinition()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 25
nc 15
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->getCurrentSubstepDefinition();
55
56
        if ($this->dataObject->getValidatedStep() === $stepService->getCurrentStep()) {
57
            if ($this->getResult()->hasErrors()) {
58
                $stepService->setCurrentSubstepDefinition($currentSubstepDefinition);
59
            } else {
60
                $nextSubstep = $this->getNextSubstep($currentSubstepDefinition);
61
62
                if ($nextSubstep) {
63
                    $stepService->setCurrentSubstepDefinition($nextSubstep);
64
                } else {
65
                    $stepService->markLastSubstepAsValidated();
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * @return SubstepDefinition
73
     */
74
    protected function getCurrentSubstepDefinition()
75
    {
76
        $stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
77
78
        $firstSubstepDefinition = $this->dataObject->getValidatedStep()->getSubsteps()->getFirstSubstepDefinition();
79
        $currentSubstepDefinition = $firstSubstepDefinition;
80
        $substepDefinition = $firstSubstepDefinition;
81
        $substepsLevel = $stepService->getSubstepsLevel();
82
83
        while ($substepsLevel > 0) {
84
            $substepsLevel--;
85
            $substepIsActivated = true;
86
87
            if ($substepDefinition->hasActivation()) {
88
                $substepIsActivated = $this->getSubstepDefinitionActivationResult($substepDefinition);
89
            }
90
91
            if (true === $substepIsActivated) {
92
                $supportedFields = $substepDefinition->getSubstep()->getSupportedFields();
93
94
                foreach ($supportedFields as $supportedField) {
95
                    $this->formValidatorExecutor->validateField($supportedField->getField());
96
                }
97
98
                if ($this->getResult()->hasErrors()) {
99
                    $currentSubstepDefinition = $substepDefinition;
100
                    break;
101
                }
102
            }
103
104
            if ($substepsLevel === 0) {
105
                $currentSubstepDefinition = $substepDefinition;
106
                break;
107
            }
108
109
            if (false === $substepDefinition->hasNextSubstep()) {
110
                break;
111
            }
112
113
            $substepDefinition = $substepDefinition->getNextSubstep();
114
        }
115
116
        return $currentSubstepDefinition;
117
    }
118
119
    protected function getNextSubstep(SubstepDefinition $substepDefinition)
120
    {
121
        $nextSubstep = null;
122
123
        while ($substepDefinition) {
124
            if (false === $substepDefinition->hasNextSubstep()) {
125
                break;
126
            } else {
127
                $substepDefinition = $substepDefinition->getNextSubstep();
128
129
                if (false === $substepDefinition->hasActivation()) {
130
                    $nextSubstep = $substepDefinition;
131
                    break;
132
                } else {
133
                    $phpResult = $this->getSubstepDefinitionActivationResult($substepDefinition);
134
135
                    if (true === $phpResult) {
136
                        $nextSubstep = $substepDefinition;
137
                        break;
138
                    }
139
                }
140
            }
141
        }
142
143
        return $nextSubstep;
144
    }
145
146
    /**
147
     * @param SubstepDefinition $substepDefinition
148
     * @return bool
149
     */
150
    protected function getSubstepDefinitionActivationResult(SubstepDefinition $substepDefinition)
151
    {
152
        $conditionProcessor = ConditionProcessorFactory::getInstance()->get($this->getFormObject());
153
        $tree = $conditionProcessor->getActivationConditionTreeForSubstep($substepDefinition);
154
        $dataObject = new PhpConditionDataObject($this->getFormObject()->getForm(), $this->formValidatorExecutor);
155
156
        return $tree->getPhpResult($dataObject);
157
    }
158
159
    /**
160
     * @return FormObject
161
     */
162
    protected function getFormObject()
163
    {
164
        return $this->dataObject->getFormObject();
165
    }
166
167
    /**
168
     * @return FormResult
169
     */
170
    protected function getResult()
171
    {
172
        return $this->dataObject->getFormResult();
173
    }
174
}
175