CreateController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 12
dl 0
loc 71
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
B defaultAction() 0 41 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\CreateBuilder,
9
    HttpResource\HttpResource,
10
    Definition\Access,
11
    Gateway
12
};
13
use Innmind\Http\{
14
    Message\Response,
15
    Message\StatusCode\StatusCode,
16
    Message\ReasonPhrase\ReasonPhrase,
17
    Headers\Headers
18
};
19
use Innmind\Filesystem\Stream\StringStream;
20
use Innmind\Immutable\{
21
    MapInterface,
22
    Set
23
};
24
use Symfony\Component\{
25
    Serializer\SerializerInterface,
26
    HttpFoundation\Request
27
};
28
29
final class CreateController
30
{
31
    private $gateways;
32
    private $serializer;
33
    private $format;
34
    private $buildHeader;
35
36 4
    public function __construct(
37
        MapInterface $gateways,
38
        SerializerInterface $serializer,
39
        Format $format,
40
        CreateBuilder $headerBuilder
41
    ) {
42
        if (
43 4
            (string) $gateways->keyType() !== 'string' ||
44 4
            (string) $gateways->valueType() !== Gateway::class
45
        ) {
46 2
            throw new \TypeError(sprintf(
47 2
                'Argument 1 must be of type MapInterface<string, %s>',
48 2
                Gateway::class
49
            ));
50
        }
51
52 2
        $this->gateways = $gateways;
53 2
        $this->serializer = $serializer;
54 2
        $this->format = $format;
55 2
        $this->buildHeader = $headerBuilder;
56 2
    }
57
58 2
    public function defaultAction(Request $request): Response
59
    {
60 2
        $definition = $request->attributes->get('_innmind_resource_definition');
61 2
        $request = $request->attributes->get('_innmind_request');
62
63
        $creator = $this
64 2
            ->gateways
65 2
            ->get((string) $definition->gateway())
66 2
            ->resourceCreator();
67 2
        $identity = $creator(
68 2
            $definition,
69 2
            $resource = $this->serializer->deserialize(
70 2
                $request,
71 2
                HttpResource::class,
72 2
                'request_'.$this->format->contentType($request)->name(),
73
                [
74 2
                    'definition' => $definition,
75 2
                    'mask' => new Access(Access::CREATE),
76
                ]
77
            )
78
        );
79
80 2
        return new Response\Response(
81 2
            $code = new StatusCode(StatusCode::codes()->get('CREATED')),
82 2
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
83 2
            $request->protocolVersion(),
84 2
            new Headers(
85 2
                ($this->buildHeader)($identity, $request, $definition, $resource)
86
            ),
87 2
            new StringStream(
88 2
                $this->serializer->serialize(
89 2
                    $identity,
90 2
                    $this->format->acceptable($request)->name(),
91
                    [
92 2
                        'request' => $request,
93 2
                        'definition' => $definition,
94
                    ]
95
                )
96
            )
97
        );
98
    }
99
}
100