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

RestApiClient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 75%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 14
c 6
b 0
f 2
lcom 1
cbo 6
dl 0
loc 144
ccs 33
cts 44
cp 0.75
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A isExecutingRequest() 0 4 1
A isProductionContextSet() 0 4 1
A executeRequest() 0 17 3
A createRequest() 0 4 1
A createRequestException() 0 12 2
A getRestlerBuilder() 0 5 1
A isRequestPreparationRequired() 0 7 3
A prepareRequest() 0 6 1
1
<?php
2
namespace Aoe\Restler\System\RestApi;
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 Aoe\Restler\System\Restler\Builder as RestlerBuilder;
30
use Aoe\Restler\System\TYPO3\Cache as Typo3Cache;
31
use Luracast\Restler\RestException;
32
use TYPO3\CMS\Core\SingletonInterface;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use stdClass;
35
36
/**
37
 * @package Restler
38
 */
39
class RestApiClient implements SingletonInterface
40
{
41
    /**
42
     * @var Typo3Cache
43
     */
44
    private $typo3Cache;
45
    /**
46
     * @var ExtensionConfiguration
47
     */
48
    private $extensionConfiguration;
49
    /**
50
     * @var boolean
51
     */
52
    private $isExecutingRequest = false;
53
    /**
54
     * @var boolean
55
     */
56
    private $isRequestPrepared = false;
57
    /**
58
     * @var RestApiRequestScope
59
     */
60
    private $restApiRequestScope;
61
62
    /**
63
     * @param ExtensionConfiguration $extensionConfiguration
64
     * @param RestApiRequestScope $restApiRequestScope
65
     * @param Typo3Cache $typo3Cache
66
     */
67 5
    public function __construct(
68
        ExtensionConfiguration $extensionConfiguration,
69
        RestApiRequestScope $restApiRequestScope,
70
        Typo3Cache $typo3Cache)
71
    {
72 5
        $this->extensionConfiguration = $extensionConfiguration;
73 5
        $this->restApiRequestScope = $restApiRequestScope;
74 5
        $this->typo3Cache = $typo3Cache;
75 5
    }
76
77
    /**
78
     * @return boolean
79
     */
80 1
    public function isExecutingRequest()
81
    {
82 1
        return $this->isExecutingRequest;
83
    }
84
85
    /**
86
     * @return boolean
87
     */
88 2
    public function isProductionContextSet()
89
    {
90 2
        return $this->extensionConfiguration->isProductionContextSet();
91
    }
92
93
    /**
94
     * @param string $requestMethod e.g. 'GET', 'POST', 'PUT' or 'DELETE'
95
     * @param string $requestUri   e.g. '/api/products/320' (without GET-params)
96
     * @param array|stdClass $getData
97
     * @param array|stdClass $postData
98
     * @return mixed can be a primitive or array or object
99
     * @throws RestApiRequestException
100
     */
101 3
    public function executeRequest($requestMethod, $requestUri, $getData = null, $postData = null)
102
    {
103 3
        if ($this->isRequestPreparationRequired()) {
104 1
            $this->prepareRequest();
105 1
        }
106
107
        try {
108 3
            $this->isExecutingRequest = true;
109 3
            $result = $this->createRequest()->executeRestApiRequest($requestMethod, $requestUri, $getData, $postData);
110 2
            $this->isExecutingRequest = false;
111 2
            return $result;
112 1
        } catch (RestException $e) {
0 ignored issues
show
Bug introduced by
The class Luracast\Restler\RestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
113 1
            $this->isExecutingRequest = false;
114 1
            $e = $this->createRequestException($e, $requestMethod, $requestUri);
115 1
            throw $e;
116
        }
117
    }
118
119
    /**
120
     * We must create for every REST-API-request a new object, because the object will contain data, which is related to the request
121
     *
122
     * @return RestApiRequest
123
     */
124
    protected function createRequest()
125
    {
126
        return new RestApiRequest($this->restApiRequestScope, $this->typo3Cache);
127
    }
128
129
    /**
130
     * @param RestException $e
131
     * @param string        $requestMethod
132
     * @param string        $requestUri
133
     * @return RestApiRequestException
134
     */
135 1
    protected function createRequestException(RestException $e, $requestMethod, $requestUri)
136
    {
137 1
        $errorMessage = 'internal REST-API-request \''.$requestMethod.':'.$requestUri.'\' could not be processed';
138 1
        if (false === $this->isProductionContextSet()) {
139
            $errorMessage .= ' (message: '.$e->getMessage().', details: '.json_encode($e->getDetails()).')';
140
        }
141 1
        return new RestApiRequestException(
142 1
            $errorMessage,
143 1
            RestApiRequestException::EXCEPTION_CODE_REQUEST_COULD_NOT_PROCESSED,
144
            $e
145 1
        );
146
    }
147
148
    /**
149
     * @return RestlerBuilder
150
     */
151
    protected function getRestlerBuilder()
152
    {
153
        $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
154
        return $objectManager->get('Aoe\\Restler\\System\\Restler\\Builder');
155
    }
156
157
    /**
158
     * We must prepare the REST-API-request when we are in the 'normal' TYPO3-context (the client, which called this PHP-request, has
159
     * NOT requested an REST-API-endpoint). In this case, we must build the 'original' REST-API-Request (aka Restler-object, which is
160
     * always required), before we can execute any REST-API-request via this PHP-client.
161
     *
162
     * @return boolean
163
     */
164
    protected function isRequestPreparationRequired()
165
    {
166
        if (defined('REST_API_IS_RUNNING') || $this->isRequestPrepared === true) {
167
            return false;
168
        }
169
        return true;
170
    }
171
172
    /**
173
     * build the 'original' REST-API-Request (aka Restler-object, which is always
174
     * required) and store it in the REST-API-Request-Scope (aka Scope-object)
175
     */
176 1
    private function prepareRequest()
177
    {
178 1
        $originalRestApiRequest = $this->getRestlerBuilder()->build();
179 1
        $this->restApiRequestScope->storeOriginalRestApiRequest($originalRestApiRequest);
180 1
        $this->isRequestPrepared = true;
181 1
    }
182
}
183