Completed
Push — develop ( 87c85a...e7e450 )
by Baptiste
03:28
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\{
7
    Format,
8
    Exception\InvalidArgumentException
9
};
10
use Innmind\Rest\Server\{
11
    Response\HeaderBuilder\CreateBuilderInterface,
12
    HttpResource,
13
    Definition\Access,
14
    GatewayInterface
15
};
16
use Innmind\Http\{
17
    Message\ResponseInterface,
18
    Message\Response,
19
    Message\StatusCode,
20
    Message\ReasonPhrase,
21
    Headers
22
};
23
use Innmind\Filesystem\Stream\StringStream;
24
use Innmind\Immutable\{
25
    MapInterface,
26
    Set
27
};
28
use Symfony\Component\{
29
    Serializer\SerializerInterface,
30
    HttpFoundation\Request
31
};
32
33
final class CreateController
34
{
35
    private $gateways;
36
    private $serializer;
37
    private $format;
38
    private $headerBuilder;
39
40 2 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
        MapInterface $gateways,
42
        SerializerInterface $serializer,
43
        Format $format,
44
        CreateBuilderInterface $headerBuilder
45
    ) {
46
        if (
47 2
            (string) $gateways->keyType() !== 'string' ||
48 2
            (string) $gateways->valueType() !== GatewayInterface::class
49
        ) {
50 1
            throw new InvalidArgumentException;
51
        }
52
53 1
        $this->gateways = $gateways;
54 1
        $this->serializer = $serializer;
55 1
        $this->format = $format;
56 1
        $this->headerBuilder = $headerBuilder;
57 1
    }
58
59 1
    public function defaultAction(Request $request): ResponseInterface
60
    {
61 1
        $definition = $request->attributes->get('_innmind_resource_definition');
62 1
        $request = $request->attributes->get('_innmind_request');
63
64
        $creator = $this
65 1
            ->gateways
66 1
            ->get((string) $definition->gateway())
67 1
            ->resourceCreator();
68 1
        $identity = $creator(
69
            $definition,
70 1
            $resource = $this->serializer->deserialize(
71
                $request,
72 1
                HttpResource::class,
73 1
                'request_'.$this->format->contentType($request)->name(),
74
                [
75 1
                    'definition' => $definition,
76 1
                    'mask' => new Access(
77 1
                        (new Set('string'))->add(Access::CREATE)
78
                    ),
79
                ]
80
            )
81
        );
82
83 1
        return new Response(
84 1
            $code = new StatusCode(StatusCode::codes()->get('CREATED')),
85 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
86 1
            $request->protocolVersion(),
87 1
            new Headers(
88 1
                $this->headerBuilder->build(
89
                    $identity,
90
                    $request,
91
                    $definition,
92
                    $resource
93
                )
94
            ),
95 1
            new StringStream(
96 1
                $this->serializer->serialize(
97
                    $identity,
98 1
                    $this->format->acceptable($request)->name(),
99
                    [
100 1
                        'request' => $request,
101 1
                        'definition' => $definition,
102
                    ]
103
                )
104
            )
105
        );
106
    }
107
}
108