Passed
Push — master ( 0ad48d...150bba )
by Yo
55s queued 11s
created

DocumentationEndpoint::httpGet()   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 0
Metric Value
eloc 6
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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
     * @throws \Exception
54
     */
55 2
    public function httpGet(Request $request) : Response
56
    {
57
        // Use Raw doc by default if not provided
58 2
        $filename = $request->get('filename') ?? RawDocProvider::SUPPORTED_FILENAME;
59 2
        $response = new Response();
60 2
        $response->headers->set('Content-Type', 'application/json');
61
62 2
        $doc = $this->normalizedDocFinder->findFor($filename, $request->getHttpHost());
63
64 2
        $response->setContent(json_encode($doc));
65
66 2
        return $response;
67
    }
68
}
69