RenderExternalViewHelper::renderStatic()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 34
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 52
rs 9.376

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Qbus\Qbtools\ViewHelpers;
3
4
use TYPO3\CMS\Core\Utility\GeneralUtility;
5
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
6
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
7
use TYPO3\CMS\Extbase\Object\ObjectManager;
8
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
9
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
10
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
11
12
/***************************************************************
13
 *  Copyright notice
14
 *
15
 *  (c) 2011 Christian Kuhn <[email protected]>
16
 *      2013 Axel Wüstemann <[email protected]>, Qbus Werbeagentur GmbH
17
 *      2015 Benjamin Franzke <[email protected]>, Qbus Werbeagentur GmbH
18
 *
19
 *  All rights reserved
20
 *
21
 *  This script is part of the TYPO3 project. The TYPO3 project is
22
 *  free software; you can redistribute it and/or modify
23
 *  it under the terms of the GNU General Public License as published by
24
 *  the Free Software Foundation; either version 3 of the License, or
25
 *  (at your option) any later version.
26
 *
27
 *  The GNU General Public License can be found at
28
 *  http://www.gnu.org/copyleft/gpl.html.
29
 *
30
 *  This script is distributed in the hope that it will be useful,
31
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 *  GNU General Public License for more details.
34
 *
35
 *  This copyright notice MUST APPEAR in all copies of the script!
36
 ***************************************************************/
37
38
/**
39
 * Renders a partial from another extension in own namespace.
40
 * based on http://pastebin.com/5NEAmqQs (c) Christian Kuhn <[email protected]> 2011
41
 *
42
 * <q:renderExternal
43
 *         partial="Device/ProductImage"
44
 *         extensionName="EnetOtherExtension"
45
 *         arguments="{
46
 *             product: entry.device.product,
47
 *             clearing: entry.device.clearing,
48
 *             maxWidth: 30,
49
 *             maxHeight: 30
50
 *         }"
51
 *     />
52
 *
53
 * @author Christian Kuhn <[email protected]>
54
 * @TODO: Implement sections
55
 */
56
class RenderExternalViewHelper extends AbstractViewHelper
57
{
58
    use CompileWithRenderStatic;
59
60
    /**
61
     * @var bool
62
     */
63
    protected $escapeOutput = false;
64
65
    /**
66
     * Initialize arguments
67
     */
68
    public function initializeArguments()
69
    {
70
        $this->registerArgument('extensionName', 'string', 'Render partial of this extension', true);
71
        $this->registerArgument('partial', 'string', 'The partial to render', false, null);
72
        $this->registerArgument('arguments', 'array', 'Arguments to pass to the partial', false, []);
73
        $this->registerArgument('pluginName', 'string', 'The pluginName of the plugin context to emulate', false, '');
74
    }
75
76
    /**
77
     * @param array $arguments
78
     * @param \Closure $renderChildrenClosure
79
     * @param RenderingContextInterface $renderingContext
80
     * @return string
81
     */
82
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
83
    {
84
        $extensionName = $arguments['extensionName'];
85
        $partial = $arguments['partial'];
86
        $args = $arguments['arguments'];
87
        $pluginName = $arguments['pluginName'];
88
89
        // Overload arguments with own extension local settings (to pass own settings to external partial)
90
        $args = self::loadSettingsIntoArguments($args, $renderingContext);
91
92
        $ctxModified = false;
93
        $backupExtensionName = '';
94
        $backupPluginName = '';
95
96
        if ($pluginName) {
97
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
98
            $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
99
            $settings = $configurationManager->getConfiguration(
100
                ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
101
                str_replace('_', '', $extensionName),
102
                $pluginName
103
            );
104
            $args['settings'] = $settings;
105
106
            $req = $renderingContext->getControllerContext()->getRequest();
0 ignored issues
show
Bug introduced by
The method getControllerContext() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. Did you maybe mean getControllerAction()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $req = $renderingContext->/** @scrutinizer ignore-call */ getControllerContext()->getRequest();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
108
            $backupPluginName = $req->getPluginName();
109
            $backupExtensionName = $req->getControllerExtensionName();
110
111
            $req->setPluginName($pluginName);
112
            $req->setControllerExtensionName(str_replace('_', '', $extensionName));
113
114
            $ctxModified = true;
115
        }
116
117
        $view = $renderingContext->getViewHelperVariableContainer()->getView();
118
119
        $oldPartialRootPaths = $view->getPartialRootPaths();
0 ignored issues
show
Bug introduced by
The method getPartialRootPaths() does not exist on TYPO3Fluid\Fluid\View\ViewInterface. It seems like you code against a sub-type of TYPO3Fluid\Fluid\View\ViewInterface such as TYPO3\CMS\Fluid\View\AbstractTemplateView. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        /** @scrutinizer ignore-call */ 
120
        $oldPartialRootPaths = $view->getPartialRootPaths();
Loading history...
120
        $newPartialRootPaths = array(
121
            ExtensionManagementUtility::extPath($extensionName) . 'Resources/Private/Partials'
122
        );
123
        $view->setPartialRootPaths($newPartialRootPaths);
0 ignored issues
show
Bug introduced by
The method setPartialRootPaths() does not exist on TYPO3Fluid\Fluid\View\ViewInterface. It seems like you code against a sub-type of TYPO3Fluid\Fluid\View\ViewInterface such as TYPO3\CMS\Fluid\View\AbstractTemplateView. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
        $view->/** @scrutinizer ignore-call */ 
124
               setPartialRootPaths($newPartialRootPaths);
Loading history...
124
        $content = $view->renderPartial($partial, null, $args);
125
        $view->setPartialRootPaths($oldPartialRootPaths);
126
127
        if ($ctxModified) {
128
            $req = $renderingContext->getControllerContext()->getRequest();
129
            $req->setPluginName($backupPluginName);
130
            $req->setControllerExtensionName($backupExtensionName);
131
        }
132
133
        return $content;
134
    }
135
136
    /**
137
     * If $arguments['settings'] is not set, it is loaded from the TemplateVariableContainer (if it is available there).
138
     *
139
     * @param  array $arguments
140
     * @param  RenderingContextInterface $renderingContext
141
     * @return array
142
     */
143
    protected static function loadSettingsIntoArguments($arguments, RenderingContextInterface $renderingContext)
144
    {
145
        if (!isset($arguments['settings']) && $renderingContext->getVariableProvider()->exists('settings')) {
146
            $arguments['settings'] = $renderingContext->getVariableProvider()->get('settings');
147
        }
148
149
        return $arguments;
150
    }
151
}
152