Passed
Push — master ( db7072...847c39 )
by Rafael
120:36 queued 75:29
created

Tsfe::initializeTsfe()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 69
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 4.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 69
ccs 37
cts 40
cp 0.925
rs 9.28
cc 4
nc 6
nop 2
crap 4.0067

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

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