Completed
Pull Request — master (#61)
by Romain
02:16
created

HasViewHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 4 1
A callRenderMethod() 0 8 2
A evaluateCondition() 0 8 1
A injectFieldService() 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\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
     * @throws ContextNotFoundException
47
     */
48
    protected function callRenderMethod()
49
    {
50
        if (false === $this->fieldService->fieldContextExists()) {
51
            throw ContextNotFoundException::slotHasViewHelperFieldContextNotFound();
52
        }
53
54
        return parent::callRenderMethod();
55
    }
56
57
    /**
58
     * @param array $arguments
59
     * @return bool
60
     */
61
    protected static function evaluateCondition($arguments = null)
62
    {
63
        /** @var SlotViewHelperService $slotService */
64
        $slotService = Core::instantiate(SlotViewHelperService::class);
65
        $slotName = $arguments['slot'];
66
67
        return $slotService->hasSlot($slotName);
68
    }
69
70
    /**
71
     * @param FieldViewHelperService $service
72
     */
73
    public function injectFieldService(FieldViewHelperService $service)
74
    {
75
        $this->fieldService = $service;
76
    }
77
}
78