Passed
Pull Request — main (#61)
by Felix
17:18 queued 08:44
created

Loader::initializeBackendUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 15
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
namespace Aoe\Restler\System\TYPO3;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2015 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Context\Context;
29
use TYPO3\CMS\Core\Core\Bootstrap;
30
use TYPO3\CMS\Core\SingletonInterface;
31
use TYPO3\CMS\Core\Site\Entity\NullSite;
32
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
33
use TYPO3\CMS\Core\TypoScript\TemplateService;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
36
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
37
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
38
use TYPO3\CMS\Frontend\Page\PageRepository;
39
use TYPO3\CMS\Frontend\Utility\EidUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Frontend\Utility\EidUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
use LogicException;
41
42
/**
43
 * @package Restler
44
 */
45
class Loader implements SingletonInterface
46
{
47
    /**
48
     * defines, if usage of backend-user is enabled
49
     *
50
     * @var boolean
51
     */
52
    private $isBackendUserInitialized = false;
53
    /**
54
     * defines, if usage of frontend-user is enabled (this is needed, if the eID-script must determine the frontend-user)
55
     *
56
     * @var boolean
57
     */
58
    private $isFrontendUserInitialized = false;
59
    /**
60
     * defines, if frontend-rendering is enabled (this is needed, if the eID-script must render some content-elements or RTE-fields)
61
     *
62
     * @var boolean
63
     */
64
    private $isFrontendRenderingInitialized = false;
65
66
    /**
67
     * @return BackendUserAuthentication
68
     * @throws LogicException
69
     */
70
    public function getBackendUser()
71
    {
72
        if ($this->isBackendUserInitialized === false) {
73
            throw new LogicException('be-user is not initialized - initialize with BE-user with method \'initializeBackendUser\'');
74
        }
75
        return $GLOBALS['BE_USER'];
76
    }
77
78
    /**
79
     * @return FrontendUserAuthentication
80
     * @throws LogicException
81
     */
82
    public function getFrontendUser()
83
    {
84
        if ($this->isFrontendUserInitialized === false) {
85
            throw new LogicException('fe-user is not initialized - initialize with FE-user with method \'initializeFrontendUser\'');
86
        }
87
        return $GLOBALS['TSFE']->fe_user;
88
    }
89
90
    /**
91
     * enable the usage of backend-user
92
     */
93
    public function initializeBackendUser()
94
    {
95
        if (!class_exists(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)) {
96
            if ($this->isBackendUserInitialized === true) {
97
                return;
98
            }
99
100
            $bootstrapObj = Bootstrap::getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance() does not exist on TYPO3\CMS\Core\Core\Bootstrap. ( Ignorable by Annotation )

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

100
            /** @scrutinizer ignore-call */ 
101
            $bootstrapObj = Bootstrap::getInstance();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            $bootstrapObj->loadExtensionTables(true);
102
            $bootstrapObj->initializeBackendUser();
103
            $bootstrapObj->initializeBackendAuthentication(true);
104
            $bootstrapObj->initializeLanguageObject();
105
        }
106
107
        $this->isBackendUserInitialized = true;
108
    }
109
110
    /**
111
     * enable the usage of frontend-user
112
     *
113
     * @param integer $pageId
114
     * @param integer $type
115
     */
116
    public function initializeFrontendUser($pageId = 0, $type = 0)
117
    {
118
        if (!class_exists(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)) {
119
            if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->fe_user)) {
120
                // FE-user is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
121
                $this->isFrontendUserInitialized = true;
122
            }
123
            if ($this->isFrontendUserInitialized === true) {
124
                return;
125
            }
126
127
            $tsfe = $this->getTsfe($pageId, $type);
128
            $tsfe->initFEUser();
0 ignored issues
show
Bug introduced by
The method initFEUser() does not exist on TYPO3\CMS\Frontend\Contr...criptFrontendController. ( Ignorable by Annotation )

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

128
            $tsfe->/** @scrutinizer ignore-call */ 
129
                   initFEUser();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
        }
130
131
        $this->isFrontendUserInitialized = true;
132
    }
133
134
    /**
135
     * enable the frontend-rendering
136
     *
137
     * @param integer $pageId
138
     * @param integer $type
139
     *
140
     * @return void
141
     */
142
    public function initializeFrontendRendering($pageId = 0, $type = 0)
143
    {
144
        if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->tmpl)) {
145
            // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
146
            $this->isFrontendRenderingInitialized = true;
147
        }
148
        if ($this->isFrontendRenderingInitialized === true) {
149
            return;
150
        }
151
152
        if (class_exists(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)) {
153
            $this->getTsfe($pageId, $type);
154
        } else {
155
            $GLOBALS['TT'] = GeneralUtility::makeInstance(TimeTracker::class);
156
157
            if ($this->isFrontendUserInitialized === false) {
158
                $this->initializeFrontendUser($pageId, $type);
159
            }
160
161
            EidUtility::initTCA();
162
163
            $tsfe = $this->getTsfe($pageId, $type);
164
            $tsfe->determineId();
165
            $tsfe->initTemplate();
0 ignored issues
show
Bug introduced by
The method initTemplate() does not exist on TYPO3\CMS\Frontend\Contr...criptFrontendController. ( Ignorable by Annotation )

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

165
            $tsfe->/** @scrutinizer ignore-call */ 
166
                   initTemplate();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
            $tsfe->getConfigArray();
167
            $tsfe->newCObj();
168
            $tsfe->calculateLinkVars();
0 ignored issues
show
Bug introduced by
The call to TYPO3\CMS\Frontend\Contr...er::calculateLinkVars() has too few arguments starting with queryParams. ( Ignorable by Annotation )

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

168
            $tsfe->/** @scrutinizer ignore-call */ 
169
                   calculateLinkVars();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
169
        }
170
171
        $this->isFrontendRenderingInitialized = true;
172
    }
173
174
    /**
175
     * @param integer $pageId
176
     * @param integer $type
177
     * @return TypoScriptFrontendController
178
     */
179
    private function getTsfe($pageId = 0, $type = 0)
180
    {
181
        if (false === array_key_exists('TSFE', $GLOBALS) && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
182
            return $GLOBALS['TSFE'];
183
        }
184
185
        if (class_exists(\TYPO3\CMS\Core\Site\Entity\NullSite::class)) {
186
            $context = GeneralUtility::makeInstance(Context::class);
187
            $nullSite = new NullSite();
188
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
189
                TypoScriptFrontendController::class,
190
                $context,
191
                $nullSite,
192
                $nullSite->getDefaultLanguage()
193
            );
194
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class, $context);
195
            $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance(TemplateService::class);
196
        } else {
197
            if ($type > 0) {
198
                $_GET['type'] = $type;
199
            }
200
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
201
                TypoScriptFrontendController::class,
202
                $GLOBALS['TYPO3_CONF_VARS'],
203
                $pageId,
204
                $type
205
            );
206
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
207
            $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance(TemplateService::class);
208
        }
209
210
        return $GLOBALS['TSFE'];
211
    }
212
}
213