Completed
Push — develop ( 99d87a...0261a6 )
by Baptiste
03:07
created

OptionsController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
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 46
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 32 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\ResponseInterface,
9
    Message\Response,
10
    Message\StatusCode,
11
    Message\ReasonPhrase,
12
    Headers,
13
    Header\HeaderInterface,
14
    Header\ContentType,
15
    Header\ContentTypeValue,
16
    Header\ParameterInterface
17
};
18
use Innmind\Filesystem\Stream\StringStream;
19
use Innmind\Immutable\Map;
20
use Symfony\Component\{
21
    HttpFoundation\Request,
22
    Serializer\SerializerInterface
23
};
24
25
final class OptionsController
26
{
27
    private $format;
28
    private $serializer;
29
30 1
    public function __construct(
31
        Format $format,
32
        SerializerInterface $serializer
33
    ) {
34 1
        $this->format = $format;
35 1
        $this->serializer = $serializer;
36 1
    }
37
38 1
    public function defaultAction(Request $request): ResponseInterface
39
    {
40 1
        $definition = $request->attributes->get('_innmind_resource_definition');
41 1
        $request = $request->attributes->get('_innmind_request');
42 1
        $format = $this->format->acceptable($request);
43 1
        $mediaType = $format->preferredMediaType();
44
45 1
        return new Response(
46 1
            $code = new StatusCode(StatusCode::codes()->get('OK')),
47 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
48 1
            $request->protocolVersion(),
49 1
            new Headers(
50 1
                (new Map('string', HeaderInterface::class))
51 1
                    ->put(
52 1
                        'Content-Type',
53 1
                        new ContentType(
54 1
                            new ContentTypeValue(
55 1
                                $mediaType->topLevel(),
56 1
                                $mediaType->subType(),
57 1
                                new Map('string', ParameterInterface::class)
58
                            )
59
                        )
60
                    )
61
            ),
62 1
            new StringStream(
63 1
                $this->serializer->serialize(
64
                    $definition,
65 1
                    $format->name()
66
                )
67
            )
68
        );
69
    }
70
}
71