Completed
Push — master ( 5d76d8...23accf )
by
unknown
04:14
created

Builder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.71%

Importance

Changes 9
Bugs 4 Features 2
Metric Value
wmc 20
c 9
b 4
f 2
lcom 1
cbo 3
dl 0
loc 153
ccs 60
cts 70
cp 0.8571
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B configureRestler() 0 35 6
A addApiClassesByGlobalArray() 0 12 4
A setAutoLoading() 0 14 2
A setCacheDirectory() 0 4 1
A __construct() 0 5 1
A build() 0 11 1
A createRestlerObject() 0 8 1
A setServerConfiguration() 0 8 4
1
<?php
2
namespace Aoe\Restler\System\Restler;
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 Aoe\Restler\Configuration\ExtensionConfiguration;
29
use InvalidArgumentException;
30
use Luracast\Restler\Defaults;
31
use Luracast\Restler\Scope;
32
use TYPO3\CMS\Core\SingletonInterface;
33
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
34
35
/**
36
 * @package Restler
37
 */
38
class Builder implements SingletonInterface
39
{
40
    /**
41
     * @var ExtensionConfiguration
42
     */
43
    private $extensionConfiguration;
44
    /**
45
     * @var ObjectManagerInterface
46
     */
47
    private $objectManager;
48
49
    /**
50
     * @param ExtensionConfiguration $extensionConfiguration
51
     * @param ObjectManagerInterface $objectManager
52
     */
53 10
    public function __construct(ExtensionConfiguration $extensionConfiguration, ObjectManagerInterface $objectManager)
54
    {
55 10
        $this->extensionConfiguration = $extensionConfiguration;
56 10
        $this->objectManager = $objectManager;
57 10
    }
58
59
    /**
60
     * initialize and configure restler-framework and return restler-object
61
     *
62
     * @return RestlerExtended
63
     */
64
    public function build()
65
    {
66
        $this->setAutoLoading();
67
        $this->setCacheDirectory();
68
        $this->setServerConfiguration();
69
70
        $restlerObj = $this->createRestlerObject();
71
        $this->configureRestler($restlerObj);
72
        $this->addApiClassesByGlobalArray($restlerObj);
73
        return $restlerObj;
74
    }
75
76
    /**
77
     * @return RestlerExtended
78
     */
79 1
    protected function createRestlerObject()
80
    {
81 1
        return new RestlerExtended(
82 1
            $this->objectManager->get('Aoe\\Restler\\System\\TYPO3\\Cache'),
83 1
            $this->extensionConfiguration->isProductionContextSet(),
84 1
            $this->extensionConfiguration->isCacheRefreshingEnabled()
85 1
        );
86
    }
87
88
    /**
89
     * Call all classes, which implements the interface 'Aoe\Restler\System\Restler\ConfigurationInterface'.
90
     * Those classes includes further restler-configurations, e.g.:
91
     *  - add API-classes
92
     *  - add authentication-classes
93
     *  - configure/set properties of several classes inside the restler-framework
94
     *  - configure overwriting of several classes inside the restler-framework
95
     *
96
     * @param RestlerExtended $restler
97
     * @throws InvalidArgumentException
98
     */
99 5
    private function configureRestler(RestlerExtended $restler)
100
    {
101 5
        $restlerConfigurationClasses = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['restler']['restlerConfigurationClasses'];
102
103 5
        if (false === is_array($restlerConfigurationClasses) || count($restlerConfigurationClasses) === 0) {
104 2
            $message = 'No restler-configuration-class found (at least one restler-configuration-class is required)! ';
105 2
            $message .= 'The configuration-class must be registered in ext_localconf.php of your TYPO3-extension like this: ';
106
            $message .= '$GLOBALS[\'TYPO3_CONF_VARS\'][\'SC_OPTIONS\'][\'restler\'][\'restlerConfigurationClasses\'][] =
107 2
                \'[YourConfigurationClass]\';';
108 2
            $message .= 'The configuration-class must implement this interface: Aoe\Restler\System\Restler\ConfigurationInterface';
109 2
            throw new InvalidArgumentException($message, 1428562059);
110
        }
111
112
        // append configuration classes from external GLOBAL registration
113 3
        if (is_array($GLOBALS['TYPO3_Restler']['restlerConfigurationClasses'])) {
114 1
            $externalRestlerConfigurationClasses = array_unique($GLOBALS['TYPO3_Restler']['restlerConfigurationClasses']);
115 1
            $restlerConfigurationClasses = array_merge(
116 1
                $restlerConfigurationClasses,
117
                $externalRestlerConfigurationClasses
118 1
            );
119 1
        }
120
121 3
        foreach ($restlerConfigurationClasses as $restlerConfigurationClass) {
122 3
            $configurationObj = $this->objectManager->get($restlerConfigurationClass);
123
124
            /* @var $configurationObj ConfigurationInterface */
125 3
            if (false === $configurationObj instanceof ConfigurationInterface) {
126 1
                $message = 'class "' . $restlerConfigurationClass . '" did not implement the ';
127 1
                $message .= 'interface "Aoe\Restler\System\Restler\ConfigurationInterface"!';
128 1
                throw new InvalidArgumentException($message, 1428562081);
129
            }
130
131 2
            $configurationObj->configureRestler($restler);
132 2
        }
133 2
    }
134
135
    /**
136
     * Add API-Controller-Classes that are registered by global array
137
     *
138
     * @param RestlerExtended $restler
139
     */
140 1
    private function addApiClassesByGlobalArray(RestlerExtended $restler)
141
    {
142 1
        $addApiController = $GLOBALS['TYPO3_Restler']['addApiClass'];
143 1
        if (is_array($addApiController)) {
144 1
            foreach ($addApiController as $apiEndpoint => $apiControllers) {
145 1
                $uniqueApiControllers = array_unique($apiControllers);
146 1
                foreach ($uniqueApiControllers as $apiController) {
147 1
                    $restler->addAPIClass($apiController, $apiEndpoint);
148 1
                }
149 1
            }
150 1
        }
151 1
    }
152
153
    /**
154
     * use auto-loading for PHP-classes of restler-framework and extBase/TYPO3 (use dependency-injection of extBase)
155
     */
156 1
    private function setAutoLoading()
157
    {
158
        // set auto-loading for restler
159 1
        $autoload = PATH_site . 'typo3conf/ext/restler/vendor/autoload.php';
160 1
        if (file_exists($autoload)) {
161
            require_once $autoload;
162
        }
163
164
        // set auto-loading for extBase/TYPO3-classes
165 1
        $objectManager = $this->objectManager;
166 1
        Scope::$resolver = function ($className) use ($objectManager) {
167 1
            return $objectManager->get($className);
168 1
        };
169 1
    }
170
171
    /**
172
     * configure cache-directory (where restler can write cache-files)
173
     */
174 1
    private function setCacheDirectory()
175
    {
176 1
        Defaults::$cacheDirectory = PATH_site . 'typo3temp/tx_restler';
177 1
    }
178
179
    /**
180
     * fix server-port (if not correct set)
181
     */
182 1
    private function setServerConfiguration()
183
    {
184 1
        if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' && $_SERVER['SERVER_PORT'] === '80') {
185
            // Fix port for HTTPS
186
            // Otherwise restler will create those urls for online-documentation, when HTTPS is used: https://www.example.com:80
187 1
            $_SERVER['SERVER_PORT'] = '443';
188 1
        }
189 1
    }
190
}
191