Issues (153)

Classes/Controller/JsonapiController.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package TYPO3
7
 */
8
9
10
namespace Aimeos\Aimeos\Controller;
11
12
13
use Aimeos\Aimeos\Base;
14
use Nyholm\Psr7\Factory\Psr17Factory;
15
16
17
/**
18
 * Controller for the frontend JSON REST API
19
 *
20
 * @package TYPO3
21
 */
22
class JsonapiController extends AbstractController
23
{
24
    /**
25
     * Dispatches the REST API requests
26
     *
27
     * @return string Generated output
28
     */
29
    public function indexAction()
30
    {
31
        $resource = $related = null;
32
33
        if ($this->request->hasArgument('resource')
34
            && ($value = $this->request->getArgument('resource')) != ''
35
        ) {
36
            $resource = $value;
37
        }
38
39
        if ($this->request->hasArgument('related')
40
            && ($value = $this->request->getArgument('related')) != ''
41
        ) {
42
            $related = $value;
43
        }
44
45
        switch ($this->request->getMethod())
46
        {
47
            case 'DELETE': return $this->deleteAction((string) $resource, $related);
48
            case 'PATCH': return $this->patchAction((string) $resource, $related);
49
            case 'POST': return $this->postAction((string) $resource, $related);
50
            case 'PUT': return $this->putAction((string) $resource, $related);
51
            case 'GET': return $this->getAction((string) $resource, $related);
52
            default: return $this->optionsAction($resource);
53
        }
54
    }
55
56
57
    /**
58
     * Deletes the resource object or a list of resource objects
59
     *
60
     * @param string Resource location, e.g. "basket"
61
     * @param string|null Related resource, e.g. "product"
0 ignored issues
show
The type Aimeos\Aimeos\Controller\Related was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
     * @return string Generated output
63
     */
64
    public function deleteAction(string $resource, string $related = null)
65
    {
66
        $response = $this->createClient($resource, $related)->delete($this->getPsrRequest(), (new Psr17Factory)->createResponse());
67
        return $this->setPsrResponse($response);
68
    }
69
70
71
    /**
72
     * Returns the requested resource object or list of resource objects
73
     *
74
     * @param string Resource location, e.g. "basket"
75
     * @param string|null Related resource, e.g. "product"
76
     * @return string Generated output
77
     */
78
    public function getAction(string $resource, string $related = null)
79
    {
80
        $response = $this->createClient($resource, $related)->get($this->getPsrRequest(), (new Psr17Factory)->createResponse());
81
        return $this->setPsrResponse($response);
82
    }
83
84
85
    /**
86
     * Updates a resource object or a list of resource objects
87
     *
88
     * @param string Resource location, e.g. "basket"
89
     * @param string|null Related resource, e.g. "product"
90
     * @return string Generated output
91
     */
92
    public function patchAction(string $resource, string $related = null)
93
    {
94
        $response = $this->createClient($resource, $related)->patch($this->getPsrRequest(), (new Psr17Factory)->createResponse());
95
        return $this->setPsrResponse($response);
96
    }
97
98
99
    /**
100
     * Creates a new resource object or a list of resource objects
101
     *
102
     * @param string Resource location, e.g. "basket"
103
     * @param string|null Related resource, e.g. "product"
104
     * @return string Generated output
105
     */
106
    public function postAction(string $resource, string $related = null)
107
    {
108
        $response = $this->createClient($resource, $related)->post($this->getPsrRequest(), (new Psr17Factory)->createResponse());
109
        return $this->setPsrResponse($response);
110
    }
111
112
113
    /**
114
     * Creates or updates a single resource object
115
     *
116
     * @param string Resource location, e.g. "basket"
117
     * @param string|null Related resource, e.g. "product"
118
     * @return string Generated output
119
     */
120
    public function putAction(string $resource, string $related = null)
121
    {
122
        $response = $this->createClient($resource, $related)->put($this->getPsrRequest(), (new Psr17Factory)->createResponse());
123
        return $this->setPsrResponse($response);
124
    }
125
126
127
    /**
128
     * Returns the available HTTP verbs and the resource URLs
129
     *
130
     * @param string Resource location, e.g. "product"
131
     * @return string Generated output
132
     */
133
    public function optionsAction(string $resource = null)
134
    {
135
        $response = $this->createClient($resource ?? '')->options($this->getPsrRequest(), (new Psr17Factory)->createResponse());
136
        return $this->setPsrResponse($response);
137
    }
138
139
140
    /**
141
     * Returns the resource client
142
     *
143
     * @param string Resource location, e.g. "basket"
144
     * @param string|null Related resource, e.g. "product"
145
     * @return \Aimeos\Client\JsonApi\Iface Jsonapi client
146
     */
147
    protected function createClient(string $resource, string $related = null) : \Aimeos\Client\JsonApi\Iface
148
    {
149
        $context = $this->context('client/jsonapi/templates');
150
        return \Aimeos\Client\JsonApi::create($context, $resource . '/' . $related);
151
    }
152
153
154
    /**
155
     * Returns a PSR-7 request object for the current request
156
     *
157
     * @return \Psr\Http\Message\RequestInterface PSR-7 request object
158
     */
159
    protected function getPsrRequest() : \Psr\Http\Message\RequestInterface
160
    {
161
        $psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
162
163
        $creator = new \Nyholm\Psr7Server\ServerRequestCreator(
164
            $psr17Factory, // ServerRequestFactory
165
            $psr17Factory, // UriFactory
166
            $psr17Factory, // UploadedFileFactory
167
            $psr17Factory  // StreamFactory
168
        );
169
170
        return $creator->fromGlobals();
171
    }
172
173
174
    /**
175
     * Set the response data from a PSR-7 response object and returns the message content
176
     *
177
     * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
178
     * @return string Generated output
179
     */
180
    protected function setPsrResponse(\Psr\Http\Message\ResponseInterface $response)
181
    {
182
        if (!isset($this->responseFactory)) // TYPO3 10
183
        {
184
            $this->response->setStatus($response->getStatusCode());
0 ignored issues
show
The property response does not exist on Aimeos\Aimeos\Controller\JsonapiController. Did you mean responseFactory?
Loading history...
185
186
            foreach ($response->getHeaders() as $key => $value) {
187
                foreach ((array) $value as $val) {
188
                    $this->response->setHeader($key, $val);
189
                }
190
            }
191
192
            return (string) $response->getBody();
193
        }
194
195
        return $response;
196
    }
197
}
198