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

StepMiddlewareValidationService   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 223
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A markStepAsValidated() 0 11 1
A addValidatedFields() 0 4 1
A stepDefinitionIsValid() 0 17 4
C getFirstInvalidStep() 0 70 8
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\Core\Core;
17
use Romm\Formz\Error\FormResult;
18
use Romm\Formz\Form\Definition\Step\Step\Step;
19
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;
20
use Romm\Formz\Form\FormObject\FormObject;
21
use Romm\Formz\Form\FormObject\Service\Step\FormStepPersistence;
22
use Romm\Formz\Middleware\Item\FormValidation\FormValidationMiddlewareOption;
23
use Romm\Formz\Validation\Validator\Form\AbstractFormValidator;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\Property\PropertyMapper;
26
use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration;
27
use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter;
28
29
class StepMiddlewareValidationService
30
{
31
    /**
32
     * @var FormObject
33
     */
34
    protected $formObject;
35
36
    /**
37
     * @var StepMiddlewareService
38
     */
39
    protected $service;
40
41
    /**
42
     * @var FormStepPersistence
43
     */
44
    protected $persistence;
45
46
    /**
47
     * @param StepMiddlewareService $service
48
     */
49
    public function __construct(StepMiddlewareService $service)
50
    {
51
        $this->service = $service;
52
        $this->formObject = $service->getFormObject();
53
        $this->persistence = $service->getStepPersistence();
54
    }
55
56
    /**
57
     * Marks the given step as validated: no errors were found during validation
58
     * with the given values array.
59
     *
60
     * @param StepDefinition $stepDefinition
61
     * @param array          $formValues
0 ignored issues
show
Bug introduced by
There is no parameter named $formValues. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     */
63
    public function markStepAsValidated(StepDefinition $stepDefinition/*, array $formValues*/) // @todo tmp-delete?
64
    {
65
        $this->persistence->markStepAsValidated($stepDefinition);
66
67
        // @todo tmp-delete?
68
//        if ($this->persistence->hasStepFormValues($stepDefinition)
69
//            && serialize($formValues) !== serialize($this->persistence->getStepFormValues($stepDefinition))
70
//        ) {
71
//            $this->persistence->resetValidationData();
72
//        }
73
    }
74
75
    /**
76
     * @param array $validatedFields
77
     */
78
    public function addValidatedFields(array $validatedFields)
79
    {
80
        $this->persistence->addValidatedFields($validatedFields);
81
    }
82
83
    /**
84
     * Checks that the previous step has already been validated, meaning the
85
     * user has the right to stand in the given step.
86
     *
87
     * @param StepDefinition $stepDefinition
88
     * @return bool
89
     */
90
    public function stepDefinitionIsValid(StepDefinition $stepDefinition)
91
    {
92
        if (false === $stepDefinition->hasPreviousDefinition()) {
93
            /*
94
             * No previous step definition found: the user stands on the first
95
             * step, it always has the right to stand there.
96
             */
97
            return true;
98
        }
99
100
        $previousStep = $stepDefinition->getPreviousDefinition()->getStep();
101
        $stepLevel = $stepDefinition->getStepLevel();
102
103
        return $this->persistence->stepWasValidated($previousStep)
104
            && true === $this->persistence->hasStepIdentifierAtLevel($stepLevel)
105
            && $stepDefinition->getStep()->getIdentifier() === $this->persistence->getStepIdentifierAtLevel($stepLevel);
106
    }
107
108
    /**
109
     * Searches for the first invalid step among previous steps from the given
110
     * step.
111
     *
112
     * All previous steps are listed, then for each one we check if submitted
113
     * form values has been saved in the step persistence, in which case the
114
     * step validation is launched again with the current form configuration.
115
     *
116
     * @param Step $step
117
     * @return StepDefinition|null
118
     */
119
    public function getFirstInvalidStep(Step $step)
120
    {
121
        $firstStep = $this->service->getFirstStepDefinition();
122
123
        if ($step === $firstStep->getStep()) {
124
            /*
125
             * The first step is always valid.
126
             */
127
            return null;
128
        }
129
130
        /*
131
         * If there is no form instance, and the request is not in the first
132
         * step, obviously the user should not be there.
133
         */
134
        if (false === $this->formObject->hasForm()) {
135
            return $firstStep;
136
        }
137
138
        /** @var StepDefinition[] $stepDefinitionsToTest */
139
        $stepDefinitionsToTest = [];
140
        $invalidStepDefinition = null;
141
        // @todo tmp-delete?
142
        /*$currentStepDefinition = */$stepDefinition = $this->service->getStepDefinition($step);
143
144
        while ($stepDefinition->hasPreviousDefinition()) {
145
            $stepDefinition = $stepDefinition->getPreviousDefinition();
146
147
            if ($stepDefinition->hasActivation()) {
148
                if (true === $this->service->getStepDefinitionConditionResult($stepDefinition)) {
149
                    array_unshift($stepDefinitionsToTest, $stepDefinition);
150
                }
151
            } else {
152
                array_unshift($stepDefinitionsToTest, $stepDefinition);
153
            }
154
        }
155
156
        foreach ($stepDefinitionsToTest as $stepDefinition) {
157
            $step = $stepDefinition->getStep();
158
159
            /*
160
             * If the already submitted form values are not found, the step is
161
             * considered as invalid.
162
             */
163
            if (false === $this->persistence->stepWasValidated($step)) {
164
                $invalidStepDefinition = $stepDefinition;
165
                break;
166
            }
167
168
            // @todo tmp-delete?
169
//            $result = $this->validateStep($step);
170
//
171
//            if ($result->hasErrors()) {
172
//                \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($result->getFlattenedErrors(), __METHOD__  . ':' . __LINE__ . ' $result->getFlattenedErrors() ');
173
//                $invalidStepDefinition = $stepDefinition;
174
//                break;
175
//            } else {
176
//                $this->persistence->markStepAsValidated($stepDefinition);
177
//                $this->persistence->addValidatedFields($result->getValidatedFields());
178
//            }
179
        }
180
181
//        $nextStepDefinition = $this->service->getNextStepDefinition($stepDefinition);
182
//
183
//        if ($nextStepDefinition !== $currentStepDefinition) {
184
//            $invalidStepDefinition = $stepDefinition;
185
//        }
186
187
        return $invalidStepDefinition;
188
    }
189
190
    // @todo tmp-delete?
191
192
//    /**
193
//     * @param array $stepFormValues
194
//     * @return PropertyMappingConfiguration
195
//     */
196
//    protected function getPropertyMappingConfiguration(array $stepFormValues)
197
//    {
198
//        /** @var PropertyMappingConfiguration $propertyMappingConfiguration */
199
//        $propertyMappingConfiguration = GeneralUtility::makeInstance(PropertyMappingConfiguration::class);
200
//        $propertyMappingConfiguration->allowAllProperties();
201
//        $propertyMappingConfiguration->setTypeConverterOption(PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, true);
202
//
203
//        foreach ($stepFormValues as $key => $value) {
204
//            if (is_array($value)) {
205
//                $propertyMappingConfiguration->forProperty($key)->allowAllProperties();
206
//            }
207
//        }
208
//
209
//        return $propertyMappingConfiguration;
210
//    }
211
//
212
//    /**
213
//     * Validates (again) the given step with the form data that were previously
214
//     * submitted and fetched from the step persistence.
215
//     *
216
//     * @param Step $step
217
//     * @return FormResult
218
//     */
219
//    protected function validateStep(Step $step)
220
//    {
221
//        /** @var PropertyMapper $propertyMapper */
222
//        $propertyMapper = Core::instantiate(PropertyMapper::class);
223
//
224
//        $stepFormValues = $this->persistence->getMergedFormValues();
225
//        $propertyMappingConfiguration = $this->getPropertyMappingConfiguration($stepFormValues);
226
//
227
//        $form = $this->formObject->getForm();
228
////        $form = $propertyMapper->convert($stepFormValues, $this->formObject->getClassName(), $propertyMappingConfiguration);
229
//
230
//        /** @var FormValidationMiddlewareOption $formValidationMiddlewareOptions */
231
//        $formValidationMiddlewareOptions = $this->formObject
232
//            ->getDefinition()
233
//            ->getPresetMiddlewares()
234
//            ->getFormValidationMiddleware()
235
//            ->getOptions();
236
//
237
//        /** @var AbstractFormValidator $validator */
238
//        $validator = Core::instantiate(
239
//            $formValidationMiddlewareOptions->getFormValidatorClassName(),
240
//            [
241
//                'name'  => $this->formObject->getName(),
242
//                'form'  => $form,
243
//                'dummy' => true
244
//            ]
245
//        );
246
//
247
//        $validator->getDataObject()->setValidatedStep($step);
248
//
249
//        return $validator->validate($form);
250
//    }
251
}
252