Passed
Push — main ( 52a7ec...5410b3 )
by Torben
03:32
created

FluidRenderingService::getTemplateFolders()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 24
nop 1
dl 0
loc 29
rs 8.8333
c 0
b 0
f 0
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\Utility\GeneralUtility;
15
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...
16
use TYPO3\CMS\Core\View\ViewFactoryInterface;
17
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...
18
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...
19
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...
20
21
class FluidRenderingService
22
{
23
    public function __construct(
24
        private readonly ConfigurationManagerInterface $configurationManager,
25
        private readonly ViewFactoryInterface $viewFactory
26
    ) {
27
    }
28
29
    /**
30
     * Renders a Fluid view for the given template
31
     */
32
    public function renderTemplate(
33
        RequestInterface $request,
34
        string $template,
35
        array $variables,
36
        string $format = 'html'
37
    ): string {
38
        $viewFactoryData = new ViewFactoryData(
39
            templateRootPaths: $this->getTemplateFolders(),
40
            partialRootPaths: $this->getTemplateFolders('partial'),
41
            layoutRootPaths: $this->getTemplateFolders('layout'),
42
            request: $request,
43
            format: $format
44
        );
45
        $view = $this->viewFactory->create($viewFactoryData);
46
        $view->assignMultiple($variables);
47
        return $view->render($template);
48
    }
49
50
    /**
51
     * Parses the given string with Fluid and decodes the result with html_entity_decode to revert Fluids encoding
52
     * of variables.
53
     *
54
     * Note, the result of this function must never be used as raw/direct output in HTML/frontend context.
55
     */
56
    public function parseString(RequestInterface $request, string $string, array $variables = []): string
57
    {
58
        if ($string === '') {
59
            return '';
60
        }
61
62
        $viewFactoryData = new ViewFactoryData(
63
            request: $request,
64
        );
65
        $view = $this->viewFactory->create($viewFactoryData);
66
        if (!$view instanceof FluidViewAdapter) {
67
            throw new \RuntimeException(
68
                'FluidRenderingService->parseStringFluid() can only deal with Fluid views via FluidViewAdapter',
69
                1727434457
70
            );
71
        }
72
        $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

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