Passed
Pull Request — master (#1317)
by
unknown
21:24
created

AbstractWidgetViewHelper::initiateSubRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.9148

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 15
cp 0.5333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
crap 3.9148
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Widget;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Widget\WidgetRequest as SolrFluidWidgetRequest;
18
use TYPO3\CMS\Extbase\Mvc\Web\Response;
19
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
20
use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode;
21
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\ChildNodeAccessInterface;
22
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper as AbstractCoreWidgetViewHelper;
23
use TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder;
24
use TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException;
25
use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest as CoreWidgetRequest;
26
use TYPO3\CMS\Fluid\Core\Widget\WidgetContext;
27
28
29
/**
30
 * Class AbstractWidgetViewHelper
31
 *
32
 * This is almost a exact copy of \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
33
 * because we need to switch the request object to overrule the widget prefix for url params
34
 * todo: find cleaner way to let widget listen to tx_solr[] instead of tx_solr[@widget]
35
 *
36
 * @author Frans Saris <[email protected]>
37
 * @author Timo Hund <[email protected]>
38
 * @package ApacheSolrForTypo3\Solr\Widget
39
 */
40
abstract class AbstractWidgetViewHelper extends AbstractCoreWidgetViewHelper implements ChildNodeAccessInterface
41
{
42
43
    /**
44
     * The Controller associated to this widget.
45
     * This needs to be filled by the individual subclass by an @inject
46
     * annotation.
47
     *
48
     * @var AbstractWidgetController
49
     * @api
50
     */
51
    protected $controller;
52
53
    /**
54
     * If set to TRUE, it is an AJAX widget.
55
     *
56
     * @var boolean
57
     * @api
58
     */
59
    protected $ajaxWidget = false;
60
61
    /**
62
     * @var AjaxWidgetContextHolder
63
     */
64
    private $ajaxWidgetContextHolder;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
65
66
    /**
67
     * @var ObjectManagerInterface
68
     */
69
    protected $objectManager;
70
71
    /**
72
     * @var \TYPO3\CMS\Extbase\Service\ExtensionService
73
     * @inject
74
     */
75
    protected $extensionService;
76
77
    /**
78
     * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
79
     */
80
    private $widgetContext;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
81
82
    /**
83
     * @var bool
84
     */
85
    protected $escapeChildren = false;
86
87
    /**
88
     * @var bool
89
     */
90
    protected $escapeOutput = false;
91
92
    /**
93
     * @param AjaxWidgetContextHolder $ajaxWidgetContextHolder
94
     * @return void
95
     */
96 24
    public function injectAjaxWidgetContextHolder(AjaxWidgetContextHolder $ajaxWidgetContextHolder)
97
    {
98 24
        $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
99 24
    }
100
101
    /**
102
     * @param ObjectManagerInterface $objectManager
103
     * @return void
104
     */
105 24
    public function injectObjectManager(ObjectManagerInterface $objectManager)
106
    {
107 24
        $this->objectManager = $objectManager;
108 24
        $this->widgetContext = $this->objectManager->get(WidgetContext::class);
109 24
    }
110
111
    /**
112
     * Initialize the arguments of the ViewHelper, and call the render() method of the ViewHelper.
113
     *
114
     * @return string the rendered ViewHelper.
115
     */
116 24
    public function initializeArgumentsAndRender()
117
    {
118 24
        $this->validateArguments();
119 24
        $this->initialize();
120 24
        $this->initializeWidgetContext();
121 24
        return $this->callRenderMethod();
122
    }
123
124
    /**
125
     * Initialize the Widget Context, before the Render method is called.
126
     *
127
     * @return void
128
     */
129 24
    private function initializeWidgetContext()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
130
    {
131 24
        $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
132 24
        $this->initializeWidgetIdentifier();
133 24
        $this->widgetContext->setControllerObjectName(get_class($this->controller));
134 24
        $extensionName = $this->controllerContext->getRequest()->getControllerExtensionName();
135 24
        $pluginName = $this->controllerContext->getRequest()->getPluginName();
136 24
        $this->widgetContext->setParentExtensionName($extensionName);
137 24
        $this->widgetContext->setParentPluginName($pluginName);
138 24
        $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
139 24
        $this->widgetContext->setParentPluginNamespace($pluginNamespace);
140 24
        $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
141 24
        if ($this->ajaxWidget === true) {
142
            $this->ajaxWidgetContextHolder->store($this->widgetContext);
143
        }
144 24
    }
145
146
    /**
147
     * Stores the syntax tree child nodes in the Widget Context, so they can be
148
     * rendered with <f:widget.renderChildren> lateron.
149
     *
150
     * @param array $childNodes The SyntaxTree Child nodes of this ViewHelper.
151
     * @return void
152
     */
153 24
    public function setChildNodes(array $childNodes)
154
    {
155 24
        $rootNode = $this->objectManager->get(RootNode::class);
156 24
        foreach ($childNodes as $childNode) {
157 24
            $rootNode->addChildNode($childNode);
158
        }
159
160 24
        $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
161 24
    }
162
163
    /**
164
     * Generate the configuration for this widget. Override to adjust.
165
     *
166
     * @return array
167
     * @api
168
     */
169 24
    protected function getWidgetConfiguration()
170
    {
171 24
        return $this->arguments;
172
    }
173
174
    /**
175
     * Initiate a sub request to $this->controller. Make sure to fill $this->controller
176
     * via Dependency Injection.
177
     *
178
     * @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface the response of this request.
179
     * @throws MissingControllerException
180
     */
181 24
    protected function initiateSubRequest()
182
    {
183 24
        if (!$this->controller instanceof AbstractWidgetController) {
184
            if (isset($this->controller)) {
185
                throw new MissingControllerException('initiateSubRequest() can not be called if there is no valid controller extending TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController. Got "' . get_class($this->controller) . '" in class "' . get_class($this) . '".', 1289422564);
186
            }
187
            throw new MissingControllerException('initiateSubRequest() can not be called if there is no controller inside $this->controller. Make sure to add a corresponding injectController method to your WidgetViewHelper class "' . get_class($this) . '".', 1284401632);
188
        }
189
            /** @var $subRequest \ApacheSolrForTypo3\Solr\Widget\WidgetRequest */
190 24
        $subRequest = $this->objectManager->get(SolrFluidWidgetRequest::class);
191 24
        $subRequest->setWidgetContext($this->widgetContext);
192 24
        $this->passArgumentsToSubRequest($subRequest);
193 24
        $subResponse = $this->objectManager->get(Response::class);
194 24
        $this->controller->processRequest($subRequest, $subResponse);
195 24
        return $subResponse;
196
    }
197
198
    /**
199
     * Pass the arguments of the widget to the subrequest.
200
     *
201
     * @param CoreWidgetRequest $subRequest
202
     * @return void
203
     */
204 24
    private function passArgumentsToSubRequest(CoreWidgetRequest $subRequest)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
205
    {
206 24
        $arguments = $this->controllerContext->getRequest()->getArguments();
207 24
        if (isset($arguments)) {
208 24
            if (isset($arguments['action'])) {
209
                $subRequest->setControllerActionName($arguments['action']);
210
                unset($arguments['action']);
211
            }
212 24
            $subRequest->setArguments($arguments);
213
        }
214 24
    }
215
216
    /**
217
     * The widget identifier is unique on the current page, and is used
218
     * in the URI as a namespace for the widget's arguments.
219
     *
220
     * @return string the widget identifier for this widget
221
     * @return void
222
     * @todo clean up, and make it somehow more routing compatible.
223
     */
224 24
    private function initializeWidgetIdentifier()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
225
    {
226 24
        $this->widgetContext->setWidgetIdentifier('');
227 24
    }
228
}
229