Completed
Push — master ( 52bda8...14effb )
by Tomas Norre
15:36
created

Loader::initializeTypo3()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
ccs 0
cts 33
cp 0
rs 8.7155
cc 6
eloc 28
nc 10
nop 0
crap 42

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 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;
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;
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 ($this->isBackEndUserInitialized === true) {
92
            return;
93
        }
94
95
        $bootstrapObj = Bootstrap::getInstance();
96
        $bootstrapObj->loadExtensionTables(true);
97
        $bootstrapObj->initializeBackendUser();
98
        $bootstrapObj->initializeBackendAuthentication();
99
        $bootstrapObj->initializeBackendUserMounts();
0 ignored issues
show
Bug introduced by
The method initializeBackendUserMounts() does not exist on TYPO3\CMS\Core\Core\Bootstrap. Did you maybe mean initializeBackendUser()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
100
        $bootstrapObj->initializeLanguageObject();
101
102
        $this->isBackEndUserInitialized = true;
103
    }
104
105
    /**
106
     * enable the usage of frontend-user
107
     *
108
     * @param integer $pageId
109
     * @param integer $type
110
     */
111
    public function initializeFrontEndUser($pageId = 0, $type = 0)
112
    {
113
        if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->fe_user)) {
114
            // FE-user is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
115
            $this->isFrontEndUserInitialized = true;
116
        }
117
        if ($this->isFrontEndUserInitialized === true) {
118
            return;
119
        }
120
121
        $tsfe = $this->getTsfe($pageId, $type);
122
        $tsfe->initFEUser();
123
        $this->isFrontEndUserInitialized = true;
124
    }
125
126
    /**
127
     * enable the frontend-rendering
128
     *
129
     * @param integer $pageId
130
     * @param integer $type
131
     *
132
     * @return void
133
     */
134
    public function initializeFrontEndRendering($pageId = 0, $type = 0)
135
    {
136
        if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->tmpl)) {
137
            // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
138
            $this->isFrontEndRenderingInitialized = true;
139
        }
140
        if ($this->isFrontEndRenderingInitialized === true) {
141
            return;
142
        }
143
144
        $GLOBALS['TT'] = new NullTimeTracker();
145
146
        if ($this->isFrontEndUserInitialized === false) {
147
            $this->initializeFrontEndUser($pageId, $type);
148
        }
149
150
        EidUtility::initTCA();
151
152
        $tsfe = $this->getTsfe($pageId, $type);
153
        $tsfe->determineId();
154
        $tsfe->initTemplate();
155
        $tsfe->getConfigArray();
156
        $tsfe->newCObj();
157
        $tsfe->calculateLinkVars();
158
        $this->isFrontEndRenderingInitialized = true;
159
    }
160
161
    /**
162
     * @param integer $pageId
163
     * @param integer $type
164
     * @return TypoScriptFrontendController
165
     */
166
    private function getTsfe($pageId, $type = 0)
167
    {
168
        if ($type > 0) {
169
            $_GET['type'] = $type;
170
        }
171
        if (false === array_key_exists('TSFE', $GLOBALS)) {
172
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
173
                'TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController',
174
                $GLOBALS['TYPO3_CONF_VARS'],
175
                $pageId,
176
                $type
177
            );
178
        }
179
        return $GLOBALS['TSFE'];
180
    }
181
}
182