Completed
Push — master ( a73fad...f8ec74 )
by Baptiste
02:31
created

UpdateController   B

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 16
dl 0
loc 67
ccs 30
cts 30
cp 1
rs 8.4614
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B defaultAction() 0 40 1
A __construct() 0 18 3
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
    public function __construct(
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