Completed
Push — feature/slots-arguments ( 2c0e8a )
by Romain
04:25
created

RenderViewHelper::renderStatic()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 13
nc 2
nop 3
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\Slot;
15
16
use Closure;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Exceptions\ContextNotFoundException;
19
use Romm\Formz\Service\ViewHelper\FieldViewHelperService;
20
use Romm\Formz\Service\ViewHelper\SlotViewHelperService;
21
use Romm\Formz\ViewHelpers\AbstractViewHelper;
22
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
23
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
24
25
/**
26
 * This is the rendering function for the `slot` view helper.
27
 *
28
 * @see \Romm\Formz\ViewHelpers\SlotViewHelper
29
 */
30
class RenderViewHelper extends AbstractViewHelper implements CompilableInterface
31
{
32
    /**
33
     * @var bool
34
     */
35
    protected $escapeOutput = false;
36
37
    /**
38
     * @var FieldViewHelperService
39
     */
40
    protected $fieldService;
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function initializeArguments()
46
    {
47
        $this->registerArgument('slot', 'string', 'Instance of the slot which will be rendered.', true);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function render()
54
    {
55
        if (false === $this->fieldService->fieldContextExists()) {
56
            throw ContextNotFoundException::slotRenderViewHelperFieldContextNotFound();
57
        }
58
59
        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...
60
    }
61
62
    /**
63
     * Will render the slot with the given name, only if the slot is found.
64
     *
65
     * @inheritdoc
66
     */
67
    public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
68
    {
69
        /** @var SlotViewHelperService $slotService */
70
        $slotService = Core::instantiate(SlotViewHelperService::class);
71
        $slotName = $arguments['slot'];
72
        $result = '';
73
74
        if ($slotService->hasSlot($slotName)) {
75
            $slotClosure = $slotService->getSlotClosure($slotName);
76
            $slotArguments = $slotService->getSlotArguments($slotName);
77
78
            foreach ($slotArguments as $key => $value) {
79
                $renderingContext->getTemplateVariableContainer()->add($key, $value);
0 ignored issues
show
Bug introduced by
The method getTemplateVariableContainer() does not seem to exist on object<TYPO3\CMS\Fluid\C...deringContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
            }
81
82
            $result = $slotClosure();
83
84
            foreach (array_keys($slotArguments) as $key) {
85
                $renderingContext->getTemplateVariableContainer()->remove($key);
0 ignored issues
show
Bug introduced by
The method getTemplateVariableContainer() does not seem to exist on object<TYPO3\CMS\Fluid\C...deringContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
            }
87
        }
88
89
        return $result;
90
    }
91
92
    /**
93
     * @param FieldViewHelperService $service
94
     */
95
    public function injectFieldService(FieldViewHelperService $service)
96
    {
97
        $this->fieldService = $service;
98
    }
99
}
100