JsonRpcHttpEndpoint::httpOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServer\Endpoint;
3
4
use Symfony\Component\HttpFoundation\Request;
5
use Symfony\Component\HttpFoundation\Response;
6
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint;
7
8
/**
9
 * Class JsonRpcHttpEndpoint
10
 */
11
class JsonRpcHttpEndpoint
12
{
13
    /** @var SdkJsonRpcEndpoint */
14
    private $sdkEndpoint;
15
16
    /** @var string[] */
17
    private $allowedMethodList = [];
18
19
    /**
20
     * @param SDKJsonRpcEndpoint $sdkEndpoint
21
     */
22 3
    public function __construct(SDKJsonRpcEndpoint $sdkEndpoint)
23
    {
24 3
        $this->sdkEndpoint = $sdkEndpoint;
25 3
        $this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS];
26
    }
27
28
    /**
29
     * @return Response
30
     */
31 1
    public function httpOptions() : Response
32
    {
33 1
        $response = new Response();
34 1
        $response->headers->set('Content-Type', 'application/json');
35
36
        // Set allowed http methods
37 1
        $allowedMethodListString = implode(', ', $this->allowedMethodList);
38 1
        $response->headers->set('Allow', $allowedMethodListString);
39 1
        $response->headers->set('Access-Control-Request-Method', $allowedMethodListString);
40
41
        // Set allowed content type
42 1
        $response->headers->set('Accept', 'application/json');
43 1
        $response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
44
45 1
        return $response;
46
    }
47
48
    /**
49
     * @param Request $request
50
     *
51
     * @return Response
52
     */
53 2
    public function httpPost(Request $request) : Response
54
    {
55 2
        $response = new Response();
56 2
        $response->headers->set('Content-Type', 'application/json');
57
58 2
        $response->setContent(
59 2
            $this->sdkEndpoint->index(
60 2
                $request->getContent()
61 2
            )
62 2
        );
63
64 2
        return $response;
65
    }
66
}
67