Failed Conditions
Pull Request — release/0.1.0 (#2)
by Yo
03:26
created

DocumentationEndpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 18
dl 0
loc 54
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 11 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
8
/**
9
 * Class DocumentationEndpoint
10
 */
11
class DocumentationEndpoint
12
{
13
    /** @var NormalizedDocFinder */
14
    private $normalizedDocFinder;
15
16
    /** @var string[] */
17
    private $allowedMethodList = [];
18
19
    /**
20
     * @param NormalizedDocFinder $normalizedDocFinder
21
     */
22 2
    public function __construct(NormalizedDocFinder $normalizedDocFinder)
23
    {
24 2
        $this->normalizedDocFinder = $normalizedDocFinder;
25 2
        $this->allowedMethodList = [Request::METHOD_GET, Request::METHOD_OPTIONS];
26 2
    }
27
28
    /**
29
     * @param Request $request
30
     *
31
     * @return Response
32
     */
33 1
    public function httpOptions(Request $request) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

33
    public function httpOptions(/** @scrutinizer ignore-unused */ Request $request) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35 1
        $response = new Response();
36 1
        $response->headers->set('Content-Type', 'application/json');
37
38
        // Set allowed http methods
39 1
        $response->headers->set('Allow', $this->allowedMethodList);
40 1
        $response->headers->set('Access-Control-Request-Method', $this->allowedMethodList);
41
42
        // Set allowed content type
43 1
        $response->headers->set('Accept', 'application/json');
44 1
        $response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
45
46 1
        return $response;
47
    }
48
49
    /**
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54 1
    public function httpGet(Request $request) : Response
55
    {
56 1
        $filename = $request->get('filename');
57 1
        $response = new Response();
58 1
        $response->headers->set('Content-Type', 'application/json');
59
60 1
        $doc = $this->normalizedDocFinder->findFor($filename, $request->getHttpHost());
0 ignored issues
show
Bug introduced by
It seems like $filename can also be of type null; however, parameter $filename of Yoanm\SymfonyJsonRpcHttp...zedDocFinder::findFor() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $doc = $this->normalizedDocFinder->findFor(/** @scrutinizer ignore-type */ $filename, $request->getHttpHost());
Loading history...
61
62 1
        $response->setContent(json_encode($doc));
63
64 1
        return $response;
65
    }
66
}
67