Completed
Push — middleware-wip ( 846df6...f76eb7 )
by Romain
05:21
created

SubstepViewHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 4 1
B render() 0 33 5
A renderStatic() 0 8 1
A injectFormService() 0 4 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\ViewHelpers\Step;
15
16
use Romm\Formz\Exceptions\ContextNotFoundException;
17
use Romm\Formz\Service\ViewHelper\FormViewHelperService;
18
use Romm\Formz\ViewHelpers\AbstractViewHelper;
19
use TYPO3\CMS\Extbase\Mvc\Web\Request;
20
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
21
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
22
23
class SubstepViewHelper extends AbstractViewHelper implements CompilableInterface
24
{
25
    /**
26
     * @var FormViewHelperService
27
     */
28
    protected $formService;
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function initializeArguments()
34
    {
35
        $this->registerArgument('identifier', 'string','Identifier of the substep.', true);
36
    }
37
38
    public function render()
39
    {
40
        /*
41
         * First, we check if this view helper is called from within the
42
         * `FormViewHelper`, because it would not make sense anywhere else.
43
         */
44
        if (false === $this->formService->formContextExists()) {
45
            throw ContextNotFoundException::substepViewHelperFormContextNotFound();
46
        }
47
48
        $formObject = $this->formService->getFormObject();
49
        $formDefinition = $formObject->getDefinition();
50
51
        if (false === $formDefinition->hasSteps()) {
52
            throw new \Exception('todo'); // @todo
53
        }
54
55
        /** @var Request $request */
56
        $request = $this->controllerContext->getRequest();
57
        $currentStep = $this->formService->getFormObject()->getCurrentStep($request);
58
59
        if (false === $currentStep->hasSubsteps()) {
60
            throw new \Exception('todo'); // @todo
61
        }
62
63
        $substeps = $currentStep->getSubsteps();
64
65
        if (false === $substeps->hasEntry($this->arguments['identifier'])) {
66
            throw new \Exception('todo'); // @todo
67
        }
68
69
        return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
0 ignored issues
show
Compatibility introduced by
$this->renderingContext of type object<TYPO3Fluid\Fluid\...deringContextInterface> is not a sub-type of object<TYPO3\CMS\Fluid\C...deringContextInterface>. It seems like you assume a child interface of the interface TYPO3Fluid\Fluid\Core\Re...nderingContextInterface 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...
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
76
    {
77
        return <<<XML
78
<div fz-substep="{$arguments['identifier']}">
79
    {$renderChildrenClosure()}
80
</div>
81
XML;
82
    }
83
84
    /**
85
     * @param FormViewHelperService $service
86
     */
87
    public function injectFormService(FormViewHelperService $service)
88
    {
89
        $this->formService = $service;
90
    }
91
}
92