Passed
Pull Request — master (#72)
by Yo
13:50
created

RequestListener::httpPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServer\EventListener;
3
4
use Symfony\Component\HttpFoundation\Request;
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\HttpKernel\Event\RequestEvent;
7
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint;
8
9
class RequestListener
10
{
11
    /** @var string */
12
    private $uri;
13
    /** @var SdkJsonRpcEndpoint */
14
    private $sdkEndpoint;
15
    /** @var string[] */
16
    private $allowedMethodList = [];
17
18 6
    public function __construct(SDKJsonRpcEndpoint $sdkEndpoint, $uri)
19
    {
20 6
        $this->uri = $uri;
21 6
        $this->sdkEndpoint = $sdkEndpoint;
22 6
        $this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS];
23
    }
24
25 6
    public function onKernelRequest(RequestEvent $event)
26
    {
27 6
        if (!$event->isMasterRequest()) {
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpKe...vent::isMasterRequest() has been deprecated: since symfony/http-kernel 5.3, use isMainRequest() instead ( Ignorable by Annotation )

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

27
        if (!/** @scrutinizer ignore-deprecated */ $event->isMasterRequest()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
28
            // Don't do anything if it's not the master request !
29 1
            return;
30
        }
31
32 5
        $request = $event->getRequest();
33 5
        if ($this->uri === $request->getRequestUri()) {
34 4
            switch ($request->getMethod()) {
35 3
                case Request::METHOD_POST:
36 2
                    $event->setResponse($this->httpPost($request));
37 2
                    break;
38 2
                case Request::METHOD_OPTIONS:
39 1
                    $event->setResponse($this->httpOptions());
40 1
                    break;
41
            }
42
        }
43
    }
44
45 1
    protected function httpOptions() : Response
46
    {
47 1
        $response = new Response();
48 1
        $response->headers->set('Content-Type', 'application/json');
49
50
        // Set allowed http methods
51 1
        $allowedMethodListString = implode(', ', $this->allowedMethodList);
52 1
        $response->headers->set('Allow', $allowedMethodListString);
53 1
        $response->headers->set('Access-Control-Request-Method', $allowedMethodListString);
54
55
        // Set allowed content type
56 1
        $response->headers->set('Accept', 'application/json');
57 1
        $response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
58
59 1
        return $response;
60
    }
61
62 2
    protected function httpPost(Request $request) : Response
63
    {
64 2
        $response = new Response();
65 2
        $response->headers->set('Content-Type', 'application/json');
66
67 2
        $response->setContent(
68 2
            $this->sdkEndpoint->index(
69 2
                $request->getContent()
70
            )
71
        );
72
73 2
        return $response;
74
    }
75
}
76