Completed
Push — TYPO3V8 ( fb3bf5...e38a52 )
by Felix
13:20 queued 12s
created

RestApiClient::createRequestException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0078
1
<?php
2
namespace Aoe\Restler\System\RestApi;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2019 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\Restler\Builder;
31
use Aoe\Restler\System\TYPO3\Cache as Typo3Cache;
32
use Luracast\Restler\RestException;
33
use TYPO3\CMS\Core\SingletonInterface;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Extbase\Object\ObjectManager;
36
use stdClass;
37
38
/**
39
 * @package Restler
40
 */
41
class RestApiClient implements SingletonInterface
42
{
43
    /**
44
     * @var Typo3Cache
45
     */
46
    private $typo3Cache;
47
    /**
48
     * @var ExtensionConfiguration
49
     */
50
    private $extensionConfiguration;
51
    /**
52
     * @var boolean
53
     */
54
    private $isExecutingRequest = false;
55
    /**
56
     * @var boolean
57
     */
58
    private $isRequestPrepared = false;
59
    /**
60
     * @var RestApiRequestScope
61
     */
62
    private $restApiRequestScope;
63
64
    /**
65
     * @param ExtensionConfiguration $extensionConfiguration
66
     * @param RestApiRequestScope $restApiRequestScope
67
     * @param Typo3Cache $typo3Cache
68
     */
69 5
    public function __construct(
70
        ExtensionConfiguration $extensionConfiguration,
71
        RestApiRequestScope $restApiRequestScope,
72
        Typo3Cache $typo3Cache
73
    ) {
74 5
        $this->extensionConfiguration = $extensionConfiguration;
75 5
        $this->restApiRequestScope = $restApiRequestScope;
76 5
        $this->typo3Cache = $typo3Cache;
77 5
    }
78
79
    /**
80
     * @return boolean
81
     */
82 1
    public function isExecutingRequest()
83
    {
84 1
        return $this->isExecutingRequest;
85
    }
86
87
    /**
88
     * @return boolean
89
     */
90 2
    public function isProductionContextSet()
91
    {
92 2
        return $this->extensionConfiguration->isProductionContextSet();
93
    }
94
95
    /**
96
     * @param string $requestMethod e.g. 'GET', 'POST', 'PUT' or 'DELETE'
97
     * @param string $requestUri   e.g. '/api/products/320' (without GET-params)
98
     * @param array|stdClass $getData
99
     * @param array|stdClass $postData
100
     * @return mixed can be a primitive or array or object
101
     * @throws RestApiRequestException
102
     */
103 3
    public function executeRequest($requestMethod, $requestUri, $getData = null, $postData = null)
104
    {
105 3
        if ($this->isRequestPreparationRequired()) {
106 1
            $this->prepareRequest();
107
        }
108
109
        try {
110 3
            $this->isExecutingRequest = true;
111 3
            $result = $this->createRequest()->executeRestApiRequest($requestMethod, $requestUri, $getData, $postData);
112 2
            $this->isExecutingRequest = false;
113 2
            return $result;
114 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...
115 1
            $this->isExecutingRequest = false;
116 1
            $e = $this->createRequestException($e, $requestMethod, $requestUri);
117 1
            throw $e;
118
        }
119
    }
120
121
    /**
122
     * We must create for every REST-API-request a new object, because the object will contain data, which is related to the request
123
     *
124
     * @return RestApiRequest
125
     */
126
    protected function createRequest()
127
    {
128
        return new RestApiRequest($this->restApiRequestScope, $this->typo3Cache);
129
    }
130
131
    /**
132
     * @param RestException $e
133
     * @param string        $requestMethod
134
     * @param string        $requestUri
135
     * @return RestApiRequestException
136
     */
137 1
    protected function createRequestException(RestException $e, $requestMethod, $requestUri)
138
    {
139 1
        $errorMessage = 'internal REST-API-request \''.$requestMethod.':'.$requestUri.'\' could not be processed';
140 1
        if (false === $this->isProductionContextSet()) {
141
            $errorMessage .= ' (message: '.$e->getMessage().', details: '.json_encode($e->getDetails()).')';
142
        }
143 1
        return new RestApiRequestException(
144 1
            $errorMessage,
145 1
            RestApiRequestException::EXCEPTION_CODE_REQUEST_COULD_NOT_PROCESSED,
146 1
            $e
147
        );
148
    }
149
150
    /**
151
     * @return RestlerBuilder
152
     */
153
    protected function getRestlerBuilder()
154
    {
155
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
156
        return $objectManager->get(Builder::class);
157
    }
158
159
    /**
160
     * We must prepare the REST-API-request when we are in the 'normal' TYPO3-context (the client, which called this PHP-request, has
161
     * NOT requested an REST-API-endpoint). In this case, we must build the 'original' REST-API-Request (aka Restler-object, which is
162
     * always required), before we can execute any REST-API-request via this PHP-client.
163
     *
164
     * @return boolean
165
     */
166
    protected function isRequestPreparationRequired()
167
    {
168
        if (defined('REST_API_IS_RUNNING') || $this->isRequestPrepared === true) {
169
            return false;
170
        }
171
        return true;
172
    }
173
174
    /**
175
     * build the 'original' REST-API-Request (aka Restler-object, which is always
176
     * required) and store it in the REST-API-Request-Scope (aka Scope-object)
177
     */
178 1
    private function prepareRequest()
179
    {
180 1
        $originalRestApiRequest = $this->getRestlerBuilder()->build();
181 1
        $this->restApiRequestScope->storeOriginalRestApiRequest($originalRestApiRequest);
182 1
        $this->isRequestPrepared = true;
183 1
    }
184
}
185