Failed Conditions
Pull Request — master (#72)
by Yo
03:08
created

RequestListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 33
c 1
b 0
f 0
dl 0
loc 66
ccs 0
cts 45
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onKernelRequest() 0 16 5
A httpPost() 0 12 1
A httpOptions() 0 15 1
A __construct() 0 6 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
    public function __construct(SDKJsonRpcEndpoint $sdkEndpoint, $uri)
19
    {
20
        var_dump("CONSTRUCT");
0 ignored issues
show
Security Debugging Code introduced by
var_dump('CONSTRUCT') looks like debug code. Are you sure you do not want to remove it?
Loading history...
21
        $this->uri = $uri;
22
        $this->sdkEndpoint = $sdkEndpoint;
23
        $this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS];
24
    }
25
26
    public function onKernelRequest(RequestEvent $event)
27
    {
28
        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

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