Failed Conditions
Pull Request — master (#75)
by Yo
02:40
created

JsonRpcHttpEndpoint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
    public function __construct(SDKJsonRpcEndpoint $sdkEndpoint)
23
    {
24
        $this->sdkEndpoint = $sdkEndpoint;
25
        $this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS];
26
    }
27
28
    /**
29
     * @return Response
30
     */
31
    public function httpOptions() : Response
32
    {
33
        $response = new Response();
34
        $response->headers->set('Content-Type', 'application/json');
35
36
        // Set allowed http methods
37
        $allowedMethodListString = implode(', ', $this->allowedMethodList);
38
        $response->headers->set('Allow', $allowedMethodListString);
39
        $response->headers->set('Access-Control-Request-Method', $allowedMethodListString);
40
41
        // Set allowed content type
42
        $response->headers->set('Accept', 'application/json');
43
        $response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
44
45
        return $response;
46
    }
47
48
    /**
49
     * @param Request $request
50
     *
51
     * @return Response
52
     */
53
    public function httpPost(Request $request) : Response
54
    {
55
        $response = new Response();
56
        $response->headers->set('Content-Type', 'application/json');
57
58
        $response->setContent(
59
            $this->sdkEndpoint->index(
60
                $request->getContent()
61
            )
62
        );
63
64
        return $response;
65
    }
66
}
67