Passed
Push — main ( 9da72c...4c09d2 )
by Felix
20:30 queued 11:53
created

RestApiClient::executeRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 3
rs 9.9
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 TYPO3\CMS\Core\Http\ServerRequest;
35
use stdClass;
36
use TYPO3\CMS\Extbase\Object\ObjectManager;
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()->executeRestApiRequest($requestMethod, $requestUri, $getData, $postData);
112 2
            $this->isExecutingRequest = false;
113 2
            return $result;
114 1
        } catch (RestException $e) {
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
        return GeneralUtility::makeInstance(ObjectManager::class)->get(RestlerBuilder::class);
156
    }
157
158
    /**
159
     * We must prepare the REST-API-request when we are in the 'normal' TYPO3-context (the client, which called this PHP-request, has
160
     * NOT requested an REST-API-endpoint). In this case, we must build the 'original' REST-API-Request (aka Restler-object, which is
161
     * always required), before we can execute any REST-API-request via this PHP-client.
162
     *
163
     * @return boolean
164
     */
165
    protected function isRequestPreparationRequired()
166
    {
167
        if (defined('REST_API_IS_RUNNING') || $this->isRequestPrepared === true) {
168
            return false;
169
        }
170
        return true;
171
    }
172
173
    /**
174
     * build the 'original' REST-API-Request (aka Restler-object, which is always
175
     * required) and store it in the REST-API-Request-Scope (aka Scope-object)
176
     */
177 1
    private function prepareRequest($requestMethod, $requestUri, $getData = null, $postData = null)
0 ignored issues
show
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

177
    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...
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

177
    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...
178
    {
179
        // TODO: pass along the post data
180 1
        $originalRestApiRequest = $this->getRestlerBuilder()->build(new ServerRequest($requestUri, $requestMethod));
181 1
        $this->restApiRequestScope->storeOriginalRestApiRequest($originalRestApiRequest);
182 1
        $this->isRequestPrepared = true;
183 1
    }
184
}
185