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

FormIdentifierHashViewHelper::renderStatic()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 8
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;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Exceptions\ContextNotFoundException;
18
use Romm\Formz\Exceptions\InvalidOptionValueException;
19
use Romm\Formz\Form\FormInterface;
20
use Romm\Formz\Form\FormObject\FormObjectFactory;
21
use Romm\Formz\Service\ViewHelper\FormViewHelperService;
22
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
23
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
24
25
/**
26
 * Use this view helper to get the identifier hash of a given form.
27
 *
28
 * If you use it inside the `FormViewHelper`, you don't have to fill the
29
 * arguments `form` and `name`.
30
 */
31
class FormIdentifierHashViewHelper extends AbstractViewHelper implements CompilableInterface
32
{
33
    /**
34
     * @inheritdoc
35
     */
36
    public function initializeArguments()
37
    {
38
        $this->registerArgument('form', 'object', 'Form instance, that must implement `FormInterface`.');
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function render()
45
    {
46
        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...
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
53
    {
54
        /** @var FormViewHelperService $formService */
55
        $formService = Core::instantiate(FormViewHelperService::class);
56
57
        $formObject = null;
58
        $form = $arguments['form'];
59
60
        if (null !== $form) {
61
            if (false === is_object($form)) {
62
                throw InvalidOptionValueException::formIdentifierViewHelperWrongFormValueType($form);
63
            }
64
65
            if (false === $form instanceof FormInterface) {
66
                throw InvalidOptionValueException::formIdentifierViewHelperWrongFormValueObjectType($form);
67
            }
68
69
            $formObject = FormObjectFactory::get()->getInstanceWithFormInstance($form);
70
        } elseif ($formService->formContextExists()) {
71
            $formObject = $formService->getFormObject();
72
        }
73
74
        if (null === $formObject) {
75
            throw ContextNotFoundException::formIdentifierViewHelperFormContextNotFound();
76
        }
77
78
        return $formObject->getFormHash();
79
    }
80
}
81