Completed
Push — bugfix/multiple-fields-level ( 9f970c )
by Romain
02:59
created

FieldViewHelperService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 0
loc 101
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setCurrentField() 0 4 1
A fieldContextExists() 0 4 1
A getCurrentField() 0 4 1
A setFieldOption() 0 4 1
A getFieldOptions() 0 4 1
A getView() 0 14 2
A resetState() 0 4 1
A getCurrentContext() 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\Service\ViewHelper\Field;
15
16
use Romm\Formz\Configuration\Form\Field\Field;
17
use Romm\Formz\Configuration\View\Layouts\Layout;
18
use Romm\Formz\Core\Core;
19
use TYPO3\CMS\Core\SingletonInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Fluid\View\StandaloneView;
22
23
/**
24
 * Contains methods to help view helpers to manipulate data concerning the
25
 * current field.
26
 */
27
class FieldViewHelperService implements SingletonInterface
28
{
29
    /**
30
     * @var FieldContextEntry[]
31
     */
32
    protected $contextEntries;
33
34
    /**
35
     * @var StandaloneView[]
36
     */
37
    protected $view;
38
39
    /**
40
     * @todo rename ?
41
     *
42
     * @param Field $field
43
     */
44
    public function setCurrentField(Field $field)
45
    {
46
        $this->contextEntries[] = GeneralUtility::makeInstance(FieldContextEntry::class, $field);
47
    }
48
49
    /**
50
     * Checks that the `FieldViewHelper` has been called. If not, an exception
51
     * is thrown.
52
     *
53
     * @return bool
54
     */
55
    public function fieldContextExists()
56
    {
57
        return false === empty($this->contextEntries);
58
    }
59
60
    /**
61
     * Returns the current field which was defined by the `FieldViewHelper`.
62
     *
63
     * Returns null if no current field was found.
64
     *
65
     * @return Field|null
66
     */
67
    public function getCurrentField()
68
    {
69
        return $this->getCurrentContext()->getField();
70
    }
71
72
    /**
73
     * @param string $name
74
     * @param mixed  $value
75
     */
76
    public function setFieldOption($name, $value)
77
    {
78
        $this->getCurrentContext()->setOption($name, $value);
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function getFieldOptions()
85
    {
86
        return $this->getCurrentContext()->getOptions();
87
    }
88
89
    /**
90
     * Returns a view instance, based on the template file of the layout. The
91
     * view is stored in local cache, to improve performance: the template file
92
     * content will be fetched only once.
93
     *
94
     * @param Layout $layout
95
     * @return StandaloneView
96
     */
97
    public function getView(Layout $layout)
98
    {
99
        $identifier = $layout->getTemplateFile();
100
101
        if (null === $this->view[$identifier]) {
102
            /** @var StandaloneView $view */
103
            $view = Core::instantiate(StandaloneView::class);
104
            $view->setTemplatePathAndFilename($layout->getTemplateFile());
105
106
            $this->view[$identifier] = $view;
107
        }
108
109
        return $this->view[$identifier];
110
    }
111
112
    /**
113
     * Reset every state that can be used by this service.
114
     */
115
    public function resetState()
116
    {
117
        array_pop($this->contextEntries);
118
    }
119
120
    /**
121
     * @return FieldContextEntry
122
     */
123
    protected function getCurrentContext()
124
    {
125
        return end($this->contextEntries);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression end($this->contextEntries); of type Romm\Formz\Service\ViewH...FieldContextEntry|false adds false to the return on line 125 which is incompatible with the return type documented by Romm\Formz\Service\ViewH...vice::getCurrentContext of type Romm\Formz\Service\ViewH...Field\FieldContextEntry. It seems like you forgot to handle an error condition.
Loading history...
126
    }
127
}
128