Completed
Push — feature/slot-has-view-helper ( b44373 )
by Romain
04:39
created

HasViewHelper::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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 Romm\Formz\Core\Core;
17
use Romm\Formz\Exceptions\ContextNotFoundException;
18
use Romm\Formz\Service\ViewHelper\FieldViewHelperService;
19
use Romm\Formz\Service\ViewHelper\SlotViewHelperService;
20
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
21
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
22
23
/**
24
 * Will check if a given slot has been defined.
25
 *
26
 * @see \Romm\Formz\ViewHelpers\SlotViewHelper
27
 */
28
class HasViewHelper extends AbstractConditionViewHelper implements CompilableInterface
29
{
30
    /**
31
     * @var FieldViewHelperService
32
     */
33
    protected $fieldService;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function initializeArguments()
39
    {
40
        $this->registerArgument('slot', 'string', 'Name of the slot.', true);
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function render()
47
    {
48
        if (false === $this->fieldService->fieldContextExists()) {
49
            throw ContextNotFoundException::slotHasViewHelperFieldContextNotFound();
50
        }
51
52
        return parent::render();
53
    }
54
55
    /**
56
     * @param array $arguments
57
     * @return boolean
58
     */
59
    protected static function evaluateCondition($arguments = null)
60
    {
61
        /** @var SlotViewHelperService $slotService */
62
        $slotService = Core::instantiate(SlotViewHelperService::class);
63
        $slotName = $arguments['slot'];
64
65
        return $slotService->hasSlotClosure($slotName);
66
    }
67
68
    /**
69
     * @param FieldViewHelperService $service
70
     */
71
    public function injectFieldService(FieldViewHelperService $service)
72
    {
73
        $this->fieldService = $service;
74
    }
75
}
76