Completed
Push — develop ( 245283...87c85a )
by Baptiste
04:30
created

RemoveController::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 4
cts 7
cp 0.5714
rs 9.4285
cc 3
eloc 9
nc 2
nop 2
crap 3.7085
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Controller\Resource;
5
6
use Innmind\Rest\ServerBundle\Exception\InvalidArgumentException;
7
use Innmind\Rest\Server\{
8
    Response\HeaderBuilder\RemoveBuilderInterface,
9
    GatewayInterface,
10
    Identity
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\MapInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
23
final class RemoveController
24
{
25
    private $gateways;
26
    private $headerBuilder;
27
28 1
    public function __construct(
29
        MapInterface $gateways,
30
        RemoveBuilderInterface $headerBuilder
31
    ) {
32
        if (
33 1
            (string) $gateways->keyType() !== 'string' ||
34 1
            (string) $gateways->valueType() !== GatewayInterface::class
35
        ) {
36 1
            throw new InvalidArgumentException;
37
        }
38
39
        $this->gateways = $gateways;
40
        $this->headerBuilder = $headerBuilder;
41
    }
42
43
    public function defaultAction(Request $request, $identity): ResponseInterface
44
    {
45
        $definition = $request->attributes->get('_innmind_resource_definition');
46
        $request = $request->attributes->get('_innmind_request');
47
48
        $remover = $this
49
            ->gateways
50
            ->get((string) $definition->gateway())
51
            ->resourceRemover();
52
        $remover(
53
            $definition,
54
            $identity = new Identity($identity)
55
        );
56
57
        return new Response(
58
            $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')),
59
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
60
            $request->protocolVersion(),
61
            new Headers(
62
                $this->headerBuilder->build(
63
                    $request,
64
                    $definition,
65
                    $identity
66
                )
67
            ),
68
            new StringStream('')
69
        );
70
    }
71
}
72