OptionsController   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 15
dl 0
loc 45
ccs 25
cts 25
cp 1
rs 9.1666
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B defaultAction() 0 31 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Controller\Resource;
5
6
use Innmind\Rest\ServerBundle\Format;
7
use Innmind\Http\{
8
    Message\Response,
9
    Message\StatusCode\StatusCode,
10
    Message\ReasonPhrase\ReasonPhrase,
11
    Headers\Headers,
12
    Header,
13
    Header\ContentType,
14
    Header\ContentTypeValue
15
};
16
use Innmind\Filesystem\Stream\StringStream;
17
use Innmind\Immutable\Map;
18
use Symfony\Component\{
19
    HttpFoundation\Request,
20
    Serializer\SerializerInterface
21
};
22
23
final class OptionsController
24
{
25
    private $format;
26
    private $serializer;
27
28 2
    public function __construct(
29
        Format $format,
30
        SerializerInterface $serializer
31
    ) {
32 2
        $this->format = $format;
33 2
        $this->serializer = $serializer;
34 2
    }
35
36 2
    public function defaultAction(Request $request): Response
37
    {
38 2
        $definition = $request->attributes->get('_innmind_resource_definition');
39 2
        $request = $request->attributes->get('_innmind_request');
40 2
        $format = $this->format->acceptable($request);
41 2
        $mediaType = $format->preferredMediaType();
42
43 2
        return new Response\Response(
44 2
            $code = new StatusCode(StatusCode::codes()->get('OK')),
45 2
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
46 2
            $request->protocolVersion(),
47 2
            new Headers(
48 2
                (new Map('string', Header::class))
49 2
                    ->put(
50 2
                        'Content-Type',
51 2
                        new ContentType(
52 2
                            new ContentTypeValue(
53 2
                                $mediaType->topLevel(),
54 2
                                $mediaType->subType()
55
                            )
56
                        )
57
                    )
58
            ),
59 2
            new StringStream(
60 2
                $this->serializer->serialize(
61 2
                    $definition,
62 2
                    $format->name()
63
                )
64
            )
65
        );
66
    }
67
}
68