FluidStandaloneService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 45
c 3
b 0
f 0
dl 0
loc 104
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A injectConfigurationManager() 0 3 1
A ensureSuffixedPath() 0 3 1
A renderTemplate() 0 26 1
B getTemplateFolders() 0 29 7
A parseStringFluid() 0 16 2
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\ServerRequest;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
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...
17
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters 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\Request;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\Request 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\StandaloneView;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Fluid\View\StandaloneView 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 FluidStandaloneService
22
{
23
    protected ConfigurationManagerInterface $configurationManager;
24
25
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
26
    {
27
        $this->configurationManager = $configurationManager;
28
    }
29
30
    /**
31
     * Returns the template folders for the given part
32
     */
33
    public function getTemplateFolders(string $part = 'template'): array
34
    {
35
        $extbaseConfig = $this->configurationManager->getConfiguration(
36
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
37
            'SfEventMgt'
38
        );
39
40
        if (!empty($extbaseConfig['view'][$part . 'RootPaths'])) {
41
            $templatePaths = $extbaseConfig['view'][$part . 'RootPaths'];
42
            ksort($templatePaths);
43
        }
44
        if (empty($templatePaths) && isset($extbaseConfig['view'])) {
45
            $path = $extbaseConfig['view'][$part . 'RootPath'];
46
            if (!empty($path)) {
47
                $templatePaths = [];
48
                $templatePaths[] = $path;
49
            }
50
        }
51
        if (empty($templatePaths)) {
52
            $templatePaths = [];
53
            $templatePaths[] = 'EXT:sf_event_mgt/Resources/Private/' . ucfirst($part) . 's/';
54
        }
55
56
        $absolutePaths = [];
57
        foreach ($templatePaths as $templatePath) {
58
            $absolutePaths[] = GeneralUtility::getFileAbsFileName($this->ensureSuffixedPath($templatePath));
59
        }
60
61
        return $absolutePaths;
62
    }
63
64
    /**
65
     * Makes sure the path ends with a slash
66
     */
67
    protected function ensureSuffixedPath(string $path): string
68
    {
69
        return rtrim($path, '/') . '/';
70
    }
71
72
    /**
73
     * Renders a fluid standalone view for the given template
74
     */
75
    public function renderTemplate(
76
        string $template,
77
        array $variables,
78
        string $extensionName = 'SfEventMgt',
79
        string $pluginName = 'Pieventregistration',
80
        string $format = 'html'
81
    ): string {
82
        $emailView = GeneralUtility::makeInstance(StandaloneView::class);
83
84
        $extbaseRequestParams = GeneralUtility::makeInstance(ExtbaseRequestParameters::class);
85
        $extbaseRequestParams->setControllerExtensionName($extensionName);
86
        $extbaseRequestParams->setPluginName($pluginName);
87
88
        /** @var ServerRequest $serverRequest */
89
        $serverRequest = $GLOBALS['TYPO3_REQUEST'];
90
91
        $extbaseRequest = GeneralUtility::makeInstance(Request::class, $serverRequest->withAttribute('extbase', $extbaseRequestParams));
92
        $emailView->setRequest($extbaseRequest);
93
94
        $emailView->setFormat($format);
95
        $emailView->setTemplateRootPaths($this->getTemplateFolders());
96
        $emailView->setLayoutRootPaths($this->getTemplateFolders('layout'));
97
        $emailView->setPartialRootPaths($this->getTemplateFolders('partial'));
98
        $emailView->setTemplate($template);
99
        $emailView->assignMultiple($variables);
100
        return $emailView->render();
101
    }
102
103
    /**
104
     * Parses the given string with Fluid View and decodes the result with html_entity_decode to revert Fluids encoding
105
     * of variables.
106
     *
107
     * Note, the result of this function must never be used as raw/direct output in HTML/frontend context.
108
     */
109
    public function parseStringFluid(string $string, array $variables = []): string
110
    {
111
        if ($string === '') {
112
            return '';
113
        }
114
115
        /** @var ServerRequest $serverRequest */
116
        $serverRequest = $GLOBALS['TYPO3_REQUEST'];
117
118
        $standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
119
        $standaloneView->setTemplateSource($string);
120
        $standaloneView->assignMultiple($variables);
121
        $standaloneView->setRequest($serverRequest);
122
        $result = $standaloneView->render() ?? '';
123
124
        return html_entity_decode($result);
125
    }
126
}
127