Passed
Push — master ( 89ac8f...d970e3 )
by Timo
20:33
created

Tsfe::initializeTsfe()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 70
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 5.0214

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 42
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 70
ccs 38
cts 42
cp 0.9048
crap 5.0214
rs 8.9368

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

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