Completed
Push — develop ( 92e89c...f8ec74 )
by Baptiste
08:17
created

UpdateController::defaultAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 21
cts 21
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
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\{
7
    Format,
8
    Exception\InvalidArgumentException
9
};
10
use Innmind\Rest\Server\{
11
    GatewayInterface,
12
    Identity,
13
    HttpResource,
14
    Definition\Access,
15
    Response\HeaderBuilder\UpdateBuilderInterface
16
};
17
use Innmind\Http\{
18
    Message\ResponseInterface,
19
    Message\Response,
20
    Message\StatusCode,
21
    Message\ReasonPhrase,
22
    Headers
23
};
24
use Innmind\Filesystem\Stream\StringStream;
25
use Innmind\Immutable\{
26
    MapInterface,
27
    Set
28
};
29
use Symfony\Component\{
30
    Serializer\SerializerInterface,
31
    HttpFoundation\Request
32
};
33
34
final class UpdateController
35
{
36
    private $gateways;
37
    private $serializer;
38
    private $format;
39
    private $headerBuilder;
40
41 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...
42
        MapInterface $gateways,
43
        SerializerInterface $serializer,
44
        Format $format,
45
        UpdateBuilderInterface $headerBuilder
46
    ) {
47
        if (
48 2
            (string) $gateways->keyType() !== 'string' ||
49 2
            (string) $gateways->valueType() !== GatewayInterface::class
50
        ) {
51 1
            throw new InvalidArgumentException;
52
        }
53
54 1
        $this->gateways = $gateways;
55 1
        $this->serializer = $serializer;
56 1
        $this->format = $format;
57 1
        $this->headerBuilder = $headerBuilder;
58 1
    }
59
60 1
    public function defaultAction(Request $request, $identity): ResponseInterface
61
    {
62 1
        $definition = $request->attributes->get('_innmind_resource_definition');
63 1
        $request = $request->attributes->get('_innmind_request');
64
65
        $updater = $this
66 1
            ->gateways
67 1
            ->get((string) $definition->gateway())
68 1
            ->resourceUpdater();
69 1
        $updater(
70
            $definition,
71 1
            $identity = new Identity($identity),
72 1
            $resource = $this->serializer->deserialize(
73
                $request,
74 1
                HttpResource::class,
75 1
                'request_'.$this->format->contentType($request)->name(),
76
                [
77 1
                    'definition' => $definition,
78 1
                    'mask' => new Access(
79 1
                        (new Set('string'))->add(Access::UPDATE)
80
                    ),
81
                ]
82
            )
83
        );
84
85 1
        return new Response(
86 1
            $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')),
87 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
88 1
            $request->protocolVersion(),
89 1
            new Headers(
90 1
                $this->headerBuilder->build(
91
                    $request,
92
                    $definition,
93
                    $identity,
94
                    $resource
95
                )
96
            ),
97 1
            new StringStream('')
98
        );
99
    }
100
}
101