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

OptionsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 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