Passed
Push — master ( 498335...52b5ef )
by Rafael
42:19
created

Tsfe::initializeTsfe()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 71
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 4.0002

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 42
c 3
b 0
f 0
dl 0
loc 71
ccs 42
cts 43
cp 0.9767
rs 9.248
cc 4
nc 6
nop 2
crap 4.0002

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

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