Passed
Push — release-11.0.x ( 534ada...88d5f5 )
by Rafael
23:03 queued 20:08
created

Tsfe   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 94.03%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
eloc 67
c 4
b 0
f 0
dl 0
loc 129
ccs 63
cts 67
cp 0.9403
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A changeLanguageContext() 0 17 5
A getAbsRefPrefixFromTSFE() 0 13 3
B initializeTsfe() 0 73 5
1
<?php
2
namespace ApacheSolrForTypo3\Solr\FrontendEnvironment;
3
4
use TYPO3\CMS\Core\Context\Context;
5
use TYPO3\CMS\Core\Context\LanguageAspectFactory;
6
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
7
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
8
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
9
use TYPO3\CMS\Core\Http\ImmediateResponseException;
10
use TYPO3\CMS\Core\SingletonInterface;
11
use TYPO3\CMS\Core\Site\SiteFinder;
12
use TYPO3\CMS\Core\TypoScript\TemplateService;
13
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
14
use TYPO3\CMS\Backend\Utility\BackendUtility;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Frontend\Page\PageRepository;
17
use TYPO3\CMS\Core\Context\UserAspect;
18
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
19
use TYPO3\CMS\Core\Http\ServerRequest;
20
use ApacheSolrForTypo3\Solr\Util;
21
22
class Tsfe implements SingletonInterface
23
{
24
25
    private $tsfeCache = [];
26
27
    private $requestCache = [];
28
29 138
    public function changeLanguageContext(int $pageId, int $language): void
30
    {
31 138
        $context = GeneralUtility::makeInstance(Context::class);
32 138
        if ($context->hasAspect('language')) {
33 138
            $hasRightLanguageId = $context->getPropertyFromAspect('language', 'id') === $language;
34 138
            $hasRightContentLanguageId = $context->getPropertyFromAspect('language', 'contentId')  === $language;
35 138
            if ($hasRightLanguageId && $hasRightContentLanguageId) {
36 138
                return;
37
            }
38
        }
39
40 17
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
41
        try {
42 17
            $site = $siteFinder->getSiteByPageId($pageId);
43 17
            $languageAspect = LanguageAspectFactory::createFromSiteLanguage($site->getLanguageById($language));
44 17
            $context->setAspect('language', $languageAspect);
45
        } catch (SiteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
46
47
        }
48 17
    }
49
50
    /**
51
     * Initializes the TSFE for a given page ID and language.
52
     *
53
     * @param $pageId
54
     * @param int $language
55
     * @throws SiteNotFoundException
56
     * @throws ServiceUnavailableException
57
     * @throws ImmediateResponseException
58
     */
59 22
    public function initializeTsfe($pageId, $language = 0)
60
    {
61
62
        // resetting, a TSFE instance with data from a different page Id could be set already
63 22
        unset($GLOBALS['TSFE']);
64
65 22
        $cacheId = $pageId . '|' . $language;
66
67
        /** @var Context $context */
68 22
        $context = GeneralUtility::makeInstance(Context::class);
69 22
        $this->changeLanguageContext((int)$pageId, (int)$language);
70
71 22
        if (!isset($this->requestCache[$cacheId])) {
72 22
            $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
73 22
            $site = $siteFinder->getSiteByPageId($pageId);
74 22
            $siteLanguage = $site->getLanguageById($language);
75
76
                /** @var ServerRequest $request */
77 22
            $request = GeneralUtility::makeInstance(ServerRequest::class);
78 22
            $this->requestCache[$cacheId] = $request
79 22
                ->withAttribute('site', $site)
80 22
                ->withAttribute('language', $siteLanguage)
81 22
                ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE)
82 22
                ->withUri($site->getBase());
83
        }
84 22
        $GLOBALS['TYPO3_REQUEST'] = $this->requestCache[$cacheId];
85
86
87 22
        if (!isset($this->tsfeCache[$cacheId])) {
88 22
            $feUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
89
            
90 22
            if (Util::getIsTYPO3VersionBelow10()) {
91
                $GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, [], $pageId, 0);
92
            } else {
93 22
                $GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $context, $site, $siteLanguage, null, $feUser);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $site does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $siteLanguage does not seem to be defined for all execution paths leading up to this point.
Loading history...
94 22
                $GLOBALS['TSFE']->id = $pageId;
95 22
                $GLOBALS['TSFE']->type = 0;
96
            }
97
98
            // for certain situations we need to trick TSFE into granting us
99
            // access to the page in any case to make getPageAndRootline() work
100
            // see http://forge.typo3.org/issues/42122
101 22
            $pageRecord = BackendUtility::getRecord('pages', $pageId, 'fe_group');
102
103 22
            $userGroups = [0, -1];
104 22
            if (!empty($pageRecord['fe_group'])) {
105
                $userGroups = array_unique(array_merge($userGroups, explode(',', $pageRecord['fe_group'])));
106
            }
107 22
            $context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $feUser, $userGroups));
108
109
            // @extensionScannerIgnoreLine
110 22
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
111 22
            $GLOBALS['TSFE']->getPageAndRootlineWithDomain($pageId, $GLOBALS['TYPO3_REQUEST']);
112
113 22
            $template = GeneralUtility::makeInstance(TemplateService::class, $context);
114 22
            $GLOBALS['TSFE']->tmpl = $template;
115 22
            $GLOBALS['TSFE']->forceTemplateParsing = true;
116 22
            $GLOBALS['TSFE']->no_cache = true;
117 22
            $GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine);
118 22
            $GLOBALS['TSFE']->no_cache = false;
119 22
            $GLOBALS['TSFE']->getConfigArray();
120 22
            $GLOBALS['TSFE']->settingLanguage();
121
122 21
            $GLOBALS['TSFE']->newCObj();
123 21
            $GLOBALS['TSFE']->absRefPrefix = self::getAbsRefPrefixFromTSFE($GLOBALS['TSFE']);
0 ignored issues
show
Bug Best Practice introduced by
The method ApacheSolrForTypo3\Solr\...tAbsRefPrefixFromTSFE() is not static, but was called statically. ( Ignorable by Annotation )

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

123
            /** @scrutinizer ignore-call */ 
124
            $GLOBALS['TSFE']->absRefPrefix = self::getAbsRefPrefixFromTSFE($GLOBALS['TSFE']);
Loading history...
124 21
            $GLOBALS['TSFE']->calculateLinkVars([]);
125
126 21
            $this->tsfeCache[$cacheId] = $GLOBALS['TSFE'];
127
        }
128
129 21
        $GLOBALS['TSFE'] = $this->tsfeCache[$cacheId];
130 21
        $GLOBALS['TSFE']->settingLocale();
131 21
        $this->changeLanguageContext((int)$pageId, (int)$language);
132 21
    }
133
134
    /**
135
     * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix
136
     * is set to "auto".
137
     */
138 21
    private function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE): string
139
    {
140 21
        $absRefPrefix = '';
141 21
        if (empty($TSFE->config['config']['absRefPrefix'])) {
142
            return $absRefPrefix;
143
        }
144
145 21
        $absRefPrefix = trim($TSFE->config['config']['absRefPrefix']);
146 21
        if ($absRefPrefix === 'auto') {
147 21
            $absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
148
        }
149
150 21
        return $absRefPrefix;
151
    }
152
}
153