Completed
Push — middleware-wip ( e5756f...1ffb75 )
by Romain
03:51
created

SubstepViewHelper::checkSubstepSupportedFields()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
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\ViewHelpers\Step;
15
16
use Romm\Formz\Exceptions\ContextNotFoundException;
17
use Romm\Formz\Form\Definition\Step\Step\Substep\Substep;
18
use Romm\Formz\Service\ViewHelper\FormViewHelperService;
19
use Romm\Formz\ViewHelpers\AbstractViewHelper;
20
use TYPO3\CMS\Extbase\Error\Error;
21
use TYPO3\CMS\Extbase\Mvc\Web\Request;
22
23
class SubstepViewHelper extends AbstractViewHelper
24
{
25
    /**
26
     * @var FormViewHelperService
27
     */
28
    protected $formService;
29
30
    /**
31
     * @var Error[]
32
     */
33
    protected $errors = [];
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function initializeArguments()
39
    {
40
        $this->registerArgument('identifier', 'string', 'Identifier of the substep.', true);
41
    }
42
43
    public function render()
44
    {
45
        /*
46
         * First, we check if this view helper is called from within the
47
         * `FormViewHelper`, because it would not make sense anywhere else.
48
         */
49
        if (false === $this->formService->formContextExists()) {
50
            throw ContextNotFoundException::substepViewHelperFormContextNotFound();
51
        }
52
53
        $formObject = $this->formService->getFormObject();
54
        $formDefinition = $formObject->getDefinition();
55
56
        if (false === $formDefinition->hasSteps()) {
57
            throw new \Exception('todo'); // @todo
58
        }
59
60
        /** @var Request $request */
61
        $request = $this->controllerContext->getRequest();
62
        $currentStep = $this->formService->getFormObject()->getCurrentStep($request);
63
64
        if (false === $currentStep->hasSubsteps()) {
65
            throw new \Exception('todo'); // @todo
66
        }
67
68
        $substeps = $currentStep->getSubsteps();
69
        $substepIdentifier = $this->arguments['identifier'];
70
71
        if (false === $substeps->hasEntry($substepIdentifier)) {
72
            throw new \Exception('todo'); // @todo
73
        }
74
75
        $currentFields = $this->formService->getCurrentFormFieldNames($this->viewHelperVariableContainer);
0 ignored issues
show
Compatibility introduced by
$this->viewHelperVariableContainer of type object<TYPO3Fluid\Fluid\...elperVariableContainer> is not a sub-type of object<TYPO3\CMS\Fluid\C...elperVariableContainer>. It seems like you assume a child class of the class TYPO3Fluid\Fluid\Core\Vi...HelperVariableContainer to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
76
77
        $content = $this->renderChildren();
78
79
        $this->checkSubstepSupportedFields($currentFields, $substeps->getEntry($substepIdentifier));
80
81
        return <<<XML
82
<div fz-substep="{$this->arguments['identifier']}">
83
    $content
84
</div>
85
XML;
86
    }
87
88
    /**
89
     * @todo
90
     *
91
     * @param array   $currentFieldsBegin
92
     * @param Substep $substep
93
     */
94
    protected function checkSubstepSupportedFields(array $currentFieldsBegin, Substep $substep)
95
    {
96
        $currentFieldsEnd = $this->formService->getCurrentFormFieldNames($this->viewHelperVariableContainer);
0 ignored issues
show
Compatibility introduced by
$this->viewHelperVariableContainer of type object<TYPO3Fluid\Fluid\...elperVariableContainer> is not a sub-type of object<TYPO3\CMS\Fluid\C...elperVariableContainer>. It seems like you assume a child class of the class TYPO3Fluid\Fluid\Core\Vi...HelperVariableContainer to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
97
        $currentFields = array_diff($currentFieldsEnd, $currentFieldsBegin);
98
99
        $formDefinition = $this->formService->getFormObject()->getDefinition();
100
101
        foreach ($currentFields as $fieldName) {
102
            if (false === $formDefinition->hasField($fieldName)) {
103
                continue;
104
            }
105
106
            $field = $formDefinition->getField($fieldName);
107
108
            if (false === $substep->supportsField($field)) {
109
                $error = new Error('todo ' . $fieldName . ' (' . $substep->getIdentifier() . ')', 123); // @todo
110
111
                $this->errors[] = $error;
112
                $this->formService->getResult()->addError($error);
113
            }
114
        }
115
    }
116
117
    /**
118
     * @param FormViewHelperService $service
119
     */
120
    public function injectFormService(FormViewHelperService $service)
121
    {
122
        $this->formService = $service;
123
    }
124
}
125