Completed
Push — develop ( 733115...529b46 )
by Baptiste
03:05
created

RemoveController::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 2
crap 3.0261
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
            throw new InvalidArgumentException;
37
        }
38
39 1
        $this->gateways = $gateways;
40 1
        $this->headerBuilder = $headerBuilder;
41 1
    }
42
43 1
    public function defaultAction(Request $request, $identity): ResponseInterface
44
    {
45 1
        $definition = $request->attributes->get('_innmind_resource_definition');
46 1
        $request = $request->attributes->get('_innmind_request');
47
48
        $remover = $this
49 1
            ->gateways
50 1
            ->get((string) $definition->gateway())
51 1
            ->resourceRemover();
52 1
        $remover(
53
            $definition,
54 1
            $identity = new Identity($identity)
55
        );
56
57 1
        return new Response(
58 1
            $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')),
59 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
60 1
            $request->protocolVersion(),
61 1
            new Headers(
62 1
                $this->headerBuilder->build(
63
                    $request,
64
                    $definition,
65
                    $identity
66
                )
67
            ),
68 1
            new StringStream('')
69
        );
70
    }
71
}
72