AbstractSwaggerValidatorAction::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Phrest\API;
4
5
abstract class AbstractSwaggerValidatorAction implements
6
    \Interop\Http\ServerMiddleware\MiddlewareInterface,
7
    \Psr\Log\LoggerAwareInterface,
8
    RequestSwaggerValidatorAwareInterface
9
{
10
    use RequestSwaggerValidatorAwareTrait;
11
    use RESTActionTrait;
12
13
    protected function onRESTRequest(\Psr\Http\Message\ServerRequestInterface $request, string $method): \Psr\Http\Message\ResponseInterface
14
    {
15
        /** @var \Zend\Expressive\Router\RouteResult $routingResult */
16
        $routingResult = $request->getAttribute(\Zend\Expressive\Router\RouteResult::class);
17
        if (!($routingResult instanceof \Zend\Expressive\Router\RouteResult)) {
18
            throw new \Phrest\Exception('request attribute "' . \Zend\Expressive\Router\RouteResult::class . '" is not a RouteResult instance');
19
        }
20
        $route = $routingResult->getMatchedRoute();
21
        if (!($route instanceof \Zend\Expressive\Router\Route)) {
22
            throw new \Phrest\Exception('no matched route found');
23
        }
24
25
        $operationId = $method.'.'.$route->getName();
26
        $operationIds = $route->getOptions()['operationIds'] ?? [];
27
        if(array_key_exists($method, $operationIds)) {
28
            $operationId = $operationIds[$method];
29
        }
30
31
        $data = $this->requestSwaggerValidator->validate($request, $operationId);
32
33
        $response = call_user_func_array([$this, $method], [$data]);
34
35
        // if there is no response -> create a response (=204 No Content)
36
        return $response ?? new \Zend\Diactoros\Response\EmptyResponse();
37
    }
38
39
    public function get(RequestSwaggerData $data): ?\Psr\Http\Message\ResponseInterface
40
    {
41
        $this->throwMethodNotAllowed('GET');
42
    }
43
44
    public function post(RequestSwaggerData $data): ?\Psr\Http\Message\ResponseInterface
45
    {
46
        $this->throwMethodNotAllowed('POST');
47
    }
48
49
    public function put(RequestSwaggerData $data): ?\Psr\Http\Message\ResponseInterface
50
    {
51
        $this->throwMethodNotAllowed('PUT');
52
    }
53
54
    public function patch(RequestSwaggerData $data): ?\Psr\Http\Message\ResponseInterface
55
    {
56
        $this->throwMethodNotAllowed('PATCH');
57
    }
58
59
    public function delete(RequestSwaggerData $data): ?\Psr\Http\Message\ResponseInterface
60
    {
61
        $this->throwMethodNotAllowed('DELETE');
62
    }
63
}
64