FluidRenderingService   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 107
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getTemplateFolders() 0 29 7
A ensureSuffixedPath() 0 3 1
A renderTemplate() 0 16 1
A __construct() 0 4 1
A parseString() 0 30 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use TYPO3\CMS\Core\Http\ApplicationType;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Core\View\ViewFactoryData;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\View\ViewFactoryData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use TYPO3\CMS\Core\View\ViewFactoryInterface;
18
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Config...urationManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use TYPO3\CMS\Fluid\View\FluidViewAdapter;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Fluid\View\FluidViewAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class FluidRenderingService
23
{
24
    public function __construct(
25
        private readonly ConfigurationManagerInterface $configurationManager,
26
        private readonly ViewFactoryInterface $viewFactory
27
    ) {
28
    }
29
30
    /**
31
     * Renders a Fluid view for the given template
32
     */
33
    public function renderTemplate(
34
        RequestInterface $request,
35
        string $template,
36
        array $variables,
37
        string $format = 'html'
38
    ): string {
39
        $viewFactoryData = new ViewFactoryData(
40
            templateRootPaths: $this->getTemplateFolders(),
41
            partialRootPaths: $this->getTemplateFolders('partial'),
42
            layoutRootPaths: $this->getTemplateFolders('layout'),
43
            request: $request,
44
            format: $format
45
        );
46
        $view = $this->viewFactory->create($viewFactoryData);
47
        $view->assignMultiple($variables);
48
        return $view->render($template);
49
    }
50
51
    /**
52
     * Parses the given string with Fluid and decodes the result with html_entity_decode to revert Fluids encoding
53
     * of variables.
54
     *
55
     * Note, the result of this function must never be used as raw/direct output in HTML/frontend context.
56
     */
57
    public function parseString(
58
        RequestInterface $request,
59
        string $string,
60
        array $variables = [],
61
        string $language = 'default'
62
    ): string {
63
        if ($string === '') {
64
            return '';
65
        }
66
67
        $isBackendRequest = ApplicationType::fromRequest($request)->isBackend();
68
        if ($isBackendRequest) {
69
            // Temporary set language of current BE user to given language
70
            $GLOBALS['BE_USER']->user['lang'] = $language;
71
        }
72
73
        $viewFactoryData = new ViewFactoryData(
74
            request: $request,
75
        );
76
        $view = $this->viewFactory->create($viewFactoryData);
77
        if (!$view instanceof FluidViewAdapter) {
78
            throw new \RuntimeException(
79
                'FluidRenderingService->parseStringFluid() can only deal with Fluid views via FluidViewAdapter',
80
                1727434457
81
            );
82
        }
83
        $view->getRenderingContext()->getTemplatePaths()->setTemplateSource($string);
0 ignored issues
show
Bug introduced by
The method getRenderingContext() does not exist on TYPO3\CMS\Core\View\ViewInterface. ( Ignorable by Annotation )

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

83
        $view->/** @scrutinizer ignore-call */ 
84
               getRenderingContext()->getTemplatePaths()->setTemplateSource($string);

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...
84
        $view->assignMultiple($variables);
85
86
        return html_entity_decode($view->render());
87
    }
88
89
    /**
90
     * Returns the template folders for the given part
91
     */
92
    protected function getTemplateFolders(string $part = 'template'): array
93
    {
94
        $extbaseConfig = $this->configurationManager->getConfiguration(
95
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
96
            'SfEventMgt'
97
        );
98
99
        if (!empty($extbaseConfig['view'][$part . 'RootPaths'])) {
100
            $templatePaths = $extbaseConfig['view'][$part . 'RootPaths'];
101
            ksort($templatePaths);
102
        }
103
        if (empty($templatePaths) && isset($extbaseConfig['view'])) {
104
            $path = $extbaseConfig['view'][$part . 'RootPath'];
105
            if (!empty($path)) {
106
                $templatePaths = [];
107
                $templatePaths[] = $path;
108
            }
109
        }
110
        if (empty($templatePaths)) {
111
            $templatePaths = [];
112
            $templatePaths[] = 'EXT:sf_event_mgt/Resources/Private/' . ucfirst($part) . 's/';
113
        }
114
115
        $absolutePaths = [];
116
        foreach ($templatePaths as $templatePath) {
117
            $absolutePaths[] = GeneralUtility::getFileAbsFileName($this->ensureSuffixedPath($templatePath));
118
        }
119
120
        return $absolutePaths;
121
    }
122
123
    /**
124
     * Makes sure the path ends with a slash
125
     */
126
    protected function ensureSuffixedPath(string $path): string
127
    {
128
        return rtrim($path, '/') . '/';
129
    }
130
}
131