Passed
Push — main ( 63b0eb...e16704 )
by Felix
03:05
created

RestApiClient::isProductionContextSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Aoe\Restler\System\RestApi;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2015 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use Aoe\Restler\Configuration\ExtensionConfiguration;
30
use Aoe\Restler\System\Restler\Builder as RestlerBuilder;
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\Core\Http\ServerRequest;
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($requestMethod, $requestUri, $getData, $postData);
107
        }
108
109
        try {
110 3
            $this->isExecutingRequest = true;
111 3
            $result = $this->createRequest()
112 3
                ->executeRestApiRequest($requestMethod, $requestUri, $getData, $postData);
113 2
            $this->isExecutingRequest = false;
114 2
            return $result;
115 1
        } catch (RestException $e) {
116 1
            $this->isExecutingRequest = false;
117 1
            $e = $this->createRequestException($e, $requestMethod, $requestUri);
118 1
            throw $e;
119
        }
120
    }
121
122
    /**
123
     * We must create for every REST-API-request a new object, because the object will contain data, which is related to the request
124
     *
125
     * @return RestApiRequest
126
     */
127
    protected function createRequest()
128
    {
129
        return new RestApiRequest($this->restApiRequestScope, $this->typo3Cache);
130
    }
131
132
    /**
133
     * @param RestException $e
134
     * @param string        $requestMethod
135
     * @param string        $requestUri
136
     * @return RestApiRequestException
137
     */
138 1
    protected function createRequestException(RestException $e, $requestMethod, $requestUri)
139
    {
140 1
        $errorMessage = 'internal REST-API-request \'' . $requestMethod . ':' . $requestUri . '\' could not be processed';
141 1
        if ($this->isProductionContextSet() === false) {
142
            $errorMessage .= ' (message: ' . $e->getMessage() . ', details: ' . json_encode($e->getDetails()) . ')';
143
        }
144 1
        return new RestApiRequestException(
145 1
            $errorMessage,
146 1
            RestApiRequestException::EXCEPTION_CODE_REQUEST_COULD_NOT_PROCESSED,
147
            $e
148
        );
149
    }
150
151
    /**
152
     * @return RestlerBuilder
153
     */
154
    protected function getRestlerBuilder()
155
    {
156
        return GeneralUtility::makeInstance(RestlerBuilder::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($requestMethod, $requestUri, $getData = null, $postData = null)
0 ignored issues
show
Unused Code introduced by
The parameter $getData is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

178
    private function prepareRequest($requestMethod, $requestUri, /** @scrutinizer ignore-unused */ $getData = null, $postData = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $postData is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

178
    private function prepareRequest($requestMethod, $requestUri, $getData = null, /** @scrutinizer ignore-unused */ $postData = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
        // TODO: pass along the post data
181 1
        $originalRestApiRequest = $this->getRestlerBuilder()
182 1
            ->build(new ServerRequest($requestUri, $requestMethod));
183 1
        $this->restApiRequestScope->storeOriginalRestApiRequest($originalRestApiRequest);
184 1
        $this->isRequestPrepared = true;
185 1
    }
186
}
187