Completed
Push — develop ( 28c4a4...33f07f )
by Baptiste
03:43
created

CreateController::defaultAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 24
cts 24
cp 1
rs 9.125
c 0
b 0
f 0
cc 1
eloc 32
nc 1
nop 1
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\CreateBuilderInterface,
9
    HttpResource,
10
    Definition\Access
11
};
12
use Innmind\Http\{
13
    Message\ResponseInterface,
14
    Message\Response,
15
    Message\StatusCode,
16
    Message\ReasonPhrase,
17
    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 $headerBuilder;
35
36 1
    public function __construct(
37
        MapInterface $gateways,
38
        SerializerInterface $serializer,
39
        Format $format,
40
        CreateBuilderInterface $headerBuilder
41
    ) {
42 1
        $this->gateways = $gateways;
43 1
        $this->serializer = $serializer;
44 1
        $this->format = $format;
45 1
        $this->headerBuilder = $headerBuilder;
46 1
    }
47
48 1
    public function defaultAction(Request $request): ResponseInterface
49
    {
50 1
        $definition = $request->attributes->get('_innmind_resource_definition');
51 1
        $request = $request->attributes->get('_innmind_request');
52
53
        $creator = $this
54 1
            ->gateways
55 1
            ->get((string) $definition->gateway())
56 1
            ->resourceCreator();
57 1
        $identity = $creator(
58
            $definition,
59 1
            $resource = $this->serializer->deserialize(
60
                $request,
61 1
                HttpResource::class,
62 1
                'request_'.$this->format->contentType($request)->name(),
63
                [
64 1
                    'definition' => $definition,
65 1
                    'mask' => new Access(
66 1
                        (new Set('string'))->add(Access::CREATE)
67
                    ),
68
                ]
69
            )
70
        );
71
72 1
        return new Response(
73 1
            $code = new StatusCode(StatusCode::codes()->get('CREATED')),
74 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
75 1
            $request->protocolVersion(),
76 1
            new Headers(
77 1
                $this->headerBuilder->build(
78
                    $identity,
79
                    $request,
80
                    $definition,
81
                    $resource
82
                )
83
            ),
84 1
            new StringStream(
85 1
                $this->serializer->serialize(
86
                    $identity,
87 1
                    $this->format->acceptable($request)->name(),
88
                    [
89 1
                        'request' => $request,
90 1
                        'definition' => $definition,
91
                    ]
92
                )
93
            )
94
        );
95
    }
96
}
97