Passed
Push — main ( 846b4b...9d0fba )
by Felix
12:18
created

Loader   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 52
c 5
b 0
f 0
dl 0
loc 146
rs 10
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFrontEndUser() 0 6 2
A getTsfe() 0 14 3
A initializeFrontEndUser() 0 16 5
A initializeBackendEndUser() 0 15 3
A getBackEndUser() 0 6 2
A initializeFrontEndRendering() 0 28 6
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\Core\Bootstrap;
29
use TYPO3\CMS\Core\SingletonInterface;
30
use TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\TimeTracker\NullTimeTracker 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...
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
33
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
34
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
35
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...
36
use LogicException;
37
38
/**
39
 * @package Restler
40
 */
41
class Loader implements SingletonInterface
42
{
43
    /**
44
     * defines, if usage of backend-user is enabled
45
     *
46
     * @var boolean
47
     */
48
    private $isBackEndUserInitialized = false;
49
    /**
50
     * defines, if usage of frontend-user is enabled (this is needed, if the eID-script must determine the frontend-user)
51
     *
52
     * @var boolean
53
     */
54
    private $isFrontEndUserInitialized = false;
55
    /**
56
     * defines, if frontend-rendering is enabled (this is needed, if the eID-script must render some content-elements or RTE-fields)
57
     *
58
     * @var boolean
59
     */
60
    private $isFrontEndRenderingInitialized = false;
61
62
    /**
63
     * @return BackendUserAuthentication
64
     * @throws LogicException
65
     */
66
    public function getBackEndUser()
67
    {
68
        if ($this->isBackEndUserInitialized === false) {
69
            throw new LogicException('be-user is not initialized - initialize with BE-user with method \'initializeBackendEndUser\'');
70
        }
71
        return $GLOBALS['BE_USER'];
72
    }
73
74
    /**
75
     * @return FrontendUserAuthentication
76
     * @throws LogicException
77
     */
78
    public function getFrontEndUser()
79
    {
80
        if ($this->isFrontEndUserInitialized === false) {
81
            throw new LogicException('fe-user is not initialized - initialize with FE-user with method \'initializeFrontEndUser\'');
82
        }
83
        return $GLOBALS['TSFE']->fe_user;
84
    }
85
86
    /**
87
     * enable the usage of backend-user
88
     */
89
    public function initializeBackendEndUser()
90
    {
91
        if(!class_exists('\TYPO3\CMS\Core\Configuration\ExtensionConfiguration')) {
92
            if ($this->isBackEndUserInitialized === true) {
93
                return;
94
            }
95
96
            $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

96
            /** @scrutinizer ignore-call */ 
97
            $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...
97
            $bootstrapObj->loadExtensionTables(true);
98
            $bootstrapObj->initializeBackendUser();
99
            $bootstrapObj->initializeBackendAuthentication(true);
100
            $bootstrapObj->initializeLanguageObject();
101
        }
102
103
        $this->isBackEndUserInitialized = true;
104
    }
105
106
    /**
107
     * enable the usage of frontend-user
108
     *
109
     * @param integer $pageId
110
     * @param integer $type
111
     */
112
    public function initializeFrontEndUser($pageId = 0, $type = 0)
113
    {
114
        if(!class_exists('\TYPO3\CMS\Core\Configuration\ExtensionConfiguration')) {
115
            if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->fe_user)) {
116
                // FE-user is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
117
                $this->isFrontEndUserInitialized = true;
118
            }
119
            if ($this->isFrontEndUserInitialized === true) {
120
                return;
121
            }
122
123
            $tsfe = $this->getTsfe($pageId, $type);
124
            $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

124
            $tsfe->/** @scrutinizer ignore-call */ 
125
                   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...
125
        }
126
127
        $this->isFrontEndUserInitialized = true;
128
    }
129
130
    /**
131
     * enable the frontend-rendering
132
     *
133
     * @param integer $pageId
134
     * @param integer $type
135
     *
136
     * @return void
137
     */
138
    public function initializeFrontEndRendering($pageId = 0, $type = 0)
139
    {
140
        if(!class_exists('\TYPO3\CMS\Core\Configuration\ExtensionConfiguration')) {
141
            if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->tmpl)) {
142
                // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
143
                $this->isFrontEndRenderingInitialized = true;
144
            }
145
            if ($this->isFrontEndRenderingInitialized === true) {
146
                return;
147
            }
148
149
            $GLOBALS['TT'] = new NullTimeTracker();
150
151
            if ($this->isFrontEndUserInitialized === false) {
152
                $this->initializeFrontEndUser($pageId, $type);
153
            }
154
155
            EidUtility::initTCA();
156
157
            $tsfe = $this->getTsfe($pageId, $type);
158
            $tsfe->determineId();
159
            $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

159
            $tsfe->/** @scrutinizer ignore-call */ 
160
                   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...
160
            $tsfe->getConfigArray();
161
            $tsfe->newCObj();
162
            $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

162
            $tsfe->/** @scrutinizer ignore-call */ 
163
                   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...
163
        }
164
165
        $this->isFrontEndRenderingInitialized = true;
166
    }
167
168
    /**
169
     * @param integer $pageId
170
     * @param integer $type
171
     * @return TypoScriptFrontendController
172
     */
173
    private function getTsfe($pageId, $type = 0)
174
    {
175
        if ($type > 0) {
176
            $_GET['type'] = $type;
177
        }
178
        if (false === array_key_exists('TSFE', $GLOBALS)) {
179
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
180
                'TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController',
181
                $GLOBALS['TYPO3_CONF_VARS'],
182
                $pageId,
183
                $type
184
            );
185
        }
186
        return $GLOBALS['TSFE'];
187
    }
188
}
189