Completed
Push — develop ( 245283...87c85a )
by Baptiste
04:30
created

OptionsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
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
    public function __construct(
31
        Format $format,
32
        SerializerInterface $serializer
33
    ) {
34
        $this->format = $format;
35
        $this->serializer = $serializer;
36
    }
37
38
    public function defaultAction(Request $request): ResponseInterface
39
    {
40
        $definition = $request->attributes->get('_innmind_resource_definition');
41
        $request = $request->attributes->get('_innmind_request');
42
        $format = $this->format->acceptable($request);
43
        $mediaType = $format->preferredMediaType();
44
45
        return new Response(
46
            $code = new StatusCode(StatusCode::codes()->get('OK')),
47
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
48
            $request->protocolVersion(),
49
            new Headers(
50
                (new Map('string', HeaderInterface::class))
51
                    ->put(
52
                        'Content-Type',
53
                        new ContentType(
54
                            new ContentTypeValue(
55
                                $mediaType->topLevel(),
56
                                $mediaType->subType(),
57
                                new Map('string', ParameterInterface::class)
58
                            )
59
                        )
60
                    )
61
            ),
62
            new StringStream(
63
                $this->serializer->serialize(
64
                    $definition,
65
                    $format->name()
66
                )
67
            )
68
        );
69
    }
70
}
71