RenderingService::renderFootnotes()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 23
ccs 0
cts 13
cp 0
rs 9.5555
cc 5
nc 5
nop 2
crap 30
1
<?php
2
3
namespace AOE\HappyFeet\Service;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2016 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use AOE\HappyFeet\Domain\Model\Footnote;
30
use AOE\HappyFeet\Domain\Repository\FootnoteRepository;
31
use TYPO3\CMS\Core\SingletonInterface;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Fluid\View\StandaloneView;
34
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
35
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
36
37
class RenderingService implements SingletonInterface
38
{
39
    /**
40
     * @var string
41
     */
42
    private const DEFAULT_TEMPLATE = 'Markup';
43
44
    private ?ContentObjectRenderer $contentObjectRenderer = null;
45
46
    public function __construct(
47
        private FootnoteRepository $footnoteRepository
48
    ) {
49
    }
50
51
    /**
52
     * @param array $conf TypoScript configuration for parseFunc
53
     */
54
    public function renderFootnotes(array $uids, array $conf = []): ?string
55
    {
56
        if ($uids === []) {
57
            return '';
58
        }
59
60
        $footnotes = $this->footnoteRepository->getFootnotesByUids($uids);
61
        if (count($footnotes) < 1) {
62
            return '';
63
        }
64
65
        /** @var Footnote $footnote */
66
        foreach ($footnotes as $footnote) {
67
            if ($footnote instanceof Footnote) {
68
                $footnote->setDescription(trim($this->renderRichText($footnote->getDescription(), $conf)));
69
            }
70
        } //render html in footnotes
71
72
        $templatePath = $this->getTemplatePath();
73
74
        $view = $this->createView($templatePath);
75
        $view->assign('footnotes', $footnotes);
76
        return $view->render();
77
    }
78
79
    /**
80
     * Renders the content of a RTE field.
81
     *
82
     * @param string $richText
83
     * @param array $conf TypoScript configuration for parseFunc
84
     * @param string $ref Reference to get configuration from. Eg. "< lib.parseFunc" which means that the configuration
85
     *                      of the object path "lib.parseFunc" will be retrieved and MERGED with what is in $conf!
86
     * @return string
87
     */
88 3
    public function renderRichText($richText, array $conf = [], ?string $ref = '< lib.parseFunc_HappyFeet')
89
    {
90 3
        if ($richText === '') {
91 1
            return '';
92
        }
93
94 2
        return $this->getContentObjectRenderer()
95 2
            ->parseFunc($richText, $conf, $ref);
96
    }
97
98
    protected function getContentObjectRenderer(): ContentObjectRenderer
99
    {
100
        if ($this->contentObjectRenderer === null) {
101
            $this->contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
102
        }
103
104
        return $this->contentObjectRenderer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->contentObjectRenderer could return the type null which is incompatible with the type-hinted return TYPO3\CMS\Frontend\Conte...t\ContentObjectRenderer. Consider adding an additional type-check to rule them out.
Loading history...
105
    }
106
107
    private function createView(string $template): StandaloneView
108
    {
109
        $view = GeneralUtility::makeInstance(StandaloneView::class);
110
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($template));
111
        return $view;
112
    }
113
114
    private function getTemplatePathAndFilename(string $template): string
115
    {
116
        return 'EXT:happy_feet/Resources/Private/Templates/Rendering/' . $template . '.html';
117
    }
118
119
    /**
120
     * checks if a alternative template is defined via typoscript setup configuration and if it exists as a file
121
     *
122
     * @return string
123
     */
124
    private function getTemplatePath()
125
    {
126
        /** @var TypoScriptFrontendController $tsfe */
127
        $tsfe = ($GLOBALS['TSFE'] ?? false);
128
        if (isset($tsfe->tmpl->setup['lib.']['plugins.']['tx_happyfeet.']['view.']['template'])) {
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ontendController::$tmpl has been deprecated. ( Ignorable by Annotation )

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

128
        if (isset(/** @scrutinizer ignore-deprecated */ $tsfe->tmpl->setup['lib.']['plugins.']['tx_happyfeet.']['view.']['template'])) {
Loading history...
129
            $templateFile = GeneralUtility::getFileAbsFileName(
130
                $tsfe->tmpl->setup['lib.']['plugins.']['tx_happyfeet.']['view.']['template']
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ontendController::$tmpl has been deprecated. ( Ignorable by Annotation )

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

130
                /** @scrutinizer ignore-deprecated */ $tsfe->tmpl->setup['lib.']['plugins.']['tx_happyfeet.']['view.']['template']
Loading history...
131
            );
132
            if (is_file($templateFile)) {
133
                return $tsfe->tmpl->setup['lib.']['plugins.']['tx_happyfeet.']['view.']['template'];
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ontendController::$tmpl has been deprecated. ( Ignorable by Annotation )

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

133
                return /** @scrutinizer ignore-deprecated */ $tsfe->tmpl->setup['lib.']['plugins.']['tx_happyfeet.']['view.']['template'];
Loading history...
134
            }
135
        }
136
137
        return $this->getTemplatePathAndFilename(self::DEFAULT_TEMPLATE);
138
    }
139
}
140