GetController::defaultAction()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 22
cts 22
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
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\Rest\Server\{
8
    Response\HeaderBuilder\GetBuilder,
9
    Gateway,
10
    Identity\Identity
11
};
12
use Innmind\Http\{
13
    Message\Response,
14
    Message\StatusCode\StatusCode,
15
    Message\ReasonPhrase\ReasonPhrase,
16
    Headers\Headers
17
};
18
use Innmind\Filesystem\Stream\StringStream;
19
use Innmind\Immutable\MapInterface;
20
use Symfony\Component\{
21
    HttpFoundation\Request,
22
    Serializer\SerializerInterface
23
};
24
25
final class GetController
26
{
27
    private $format;
28
    private $serializer;
29
    private $gateways;
30
    private $buildHeader;
31
32 4
    public function __construct(
33
        Format $format,
34
        SerializerInterface $serializer,
35
        MapInterface $gateways,
36
        GetBuilder $headerBuilder
37
    ) {
38
        if (
39 4
            (string) $gateways->keyType() !== 'string' ||
40 4
            (string) $gateways->valueType() !== Gateway::class
41
        ) {
42 2
            throw new \TypeError(sprintf(
43 2
                'Argument 3 must be of type MapInterface<string, %s>',
44 2
                Gateway::class
45
            ));
46
        }
47
48 2
        $this->format = $format;
49 2
        $this->serializer = $serializer;
50 2
        $this->gateways = $gateways;
51 2
        $this->buildHeader = $headerBuilder;
52 2
    }
53
54 2
    public function defaultAction(Request $request, $identity): Response
55
    {
56 2
        $definition = $request->attributes->get('_innmind_resource_definition');
57 2
        $request = $request->attributes->get('_innmind_request');
58
59
        $accessor = $this
60 2
            ->gateways
61 2
            ->get((string) $definition->gateway())
62 2
            ->resourceAccessor();
63 2
        $resource = $accessor(
64 2
            $definition,
65 2
            $identity = new Identity($identity)
66
        );
67
68 2
        return new Response\Response(
69 2
            $code = new StatusCode(StatusCode::codes()->get('OK')),
70 2
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
71 2
            $request->protocolVersion(),
72 2
            new Headers(
73 2
                ($this->buildHeader)($resource, $request, $definition, $identity)
74
            ),
75 2
            new StringStream(
76 2
                $this->serializer->serialize(
77 2
                    $resource,
78 2
                    $this->format->acceptable($request)->name(),
79
                    [
80 2
                        'request' => $request,
81 2
                        'definition' => $definition,
82 2
                        'identity' => $identity,
83
                    ]
84
                )
85
            )
86
        );
87
    }
88
}
89