Passed
Push — release/0.1.0 ( 0ad48d...de46b4 )
by Yo
03:02
created

DocumentationEndpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 18
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A httpOptions() 0 14 1
A httpGet() 0 12 1
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\Endpoint;
3
4
use Symfony\Component\HttpFoundation\Request;
5
use Symfony\Component\HttpFoundation\Response;
6
use Yoanm\SymfonyJsonRpcHttpServerDoc\Finder\NormalizedDocFinder;
7
use Yoanm\SymfonyJsonRpcHttpServerDoc\Provider\RawDocProvider;
8
9
/**
10
 * Class DocumentationEndpoint
11
 */
12
class DocumentationEndpoint
13
{
14
    /** @var NormalizedDocFinder */
15
    private $normalizedDocFinder;
16
17
    /** @var string[] */
18
    private $allowedMethodList = [];
19
20
    /**
21
     * @param NormalizedDocFinder $normalizedDocFinder
22
     */
23 3
    public function __construct(NormalizedDocFinder $normalizedDocFinder)
24
    {
25 3
        $this->normalizedDocFinder = $normalizedDocFinder;
26 3
        $this->allowedMethodList = [Request::METHOD_GET, Request::METHOD_OPTIONS];
27 3
    }
28
29
    /**
30
     * @return Response
31
     */
32 1
    public function httpOptions() : Response
33
    {
34 1
        $response = new Response();
35 1
        $response->headers->set('Content-Type', 'application/json');
36
37
        // Set allowed http methods
38 1
        $response->headers->set('Allow', $this->allowedMethodList);
39 1
        $response->headers->set('Access-Control-Request-Method', $this->allowedMethodList);
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 httpGet(Request $request) : Response
54
    {
55
        // Use Raw doc by default if not provided
56 2
        $filename = $request->get('filename') ?? RawDocProvider::SUPPORTED_FILENAME;
57 2
        $response = new Response();
58 2
        $response->headers->set('Content-Type', 'application/json');
59
60 2
        $doc = $this->normalizedDocFinder->findFor($filename, $request->getHttpHost());
61
62 2
        $response->setContent(json_encode($doc));
63
64 2
        return $response;
65
    }
66
}
67