Completed
Push — develop ( ffebe5...25764f )
by Baptiste
04:27
created

UnlinkController::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 2
nop 3
crap 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Controller\Resource;
5
6
use Innmind\Rest\ServerBundle\{
7
    Exception\InvalidArgumentException,
8
    Translator\LinkTranslator
9
};
10
use Innmind\Rest\Server\{
11
    Response\HeaderBuilder\UnlinkBuilderInterface,
12
    GatewayInterface,
13
    Reference,
14
    Identity,
15
    Link\ParameterInterface,
16
    Link\Parameter
17
};
18
use Innmind\Http\{
19
    Message\ResponseInterface,
20
    Message\Response,
21
    Message\StatusCode,
22
    Message\ReasonPhrase,
23
    Headers,
24
    Header\Link,
25
    Header\LinkValue,
26
    Header\ParameterInterface as LinkParameterInterface
27
};
28
use Innmind\Filesystem\Stream\StringStream;
29
use Innmind\Immutable\{
30
    MapInterface,
31
    Map
32
};
33
use Symfony\Component\HttpFoundation\Request;
34
35 View Code Duplication
final class UnlinkController
0 ignored issues
show
Duplication introduced by
This class 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...
36
{
37
    private $gateways;
38
    private $headerBuilder;
39
    private $translator;
40
41 2
    public function __construct(
42
        MapInterface $gateways,
43
        UnlinkBuilderInterface $headerBuilder,
44
        LinkTranslator $translator
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->headerBuilder = $headerBuilder;
55 1
        $this->translator = $translator;
56 1
    }
57
58 1
    public function defaultAction(Request $request, $identity): ResponseInterface
59
    {
60 1
        $from = $request->attributes->get('_innmind_resource_definition');
61 1
        $request = $request->attributes->get('_innmind_request');
62 1
        $tos = $this->translator->translate($request->headers()->get('Link'));
63
64
        $unlinker = $this
65 1
            ->gateways
66 1
            ->get((string) $from->gateway())
67 1
            ->resourceUnlinker();
68 1
        $unlinker(
69 1
            $from = new Reference($from, new Identity($identity)),
70
            $tos
71
        );
72
73 1
        return new Response(
74 1
            $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')),
75 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
76 1
            $request->protocolVersion(),
77 1
            new Headers(
78 1
                $this->headerBuilder->build(
79
                    $request,
80
                    $from,
81
                    $tos
82
                )
83
            ),
84 1
            new StringStream('')
85
        );
86
    }
87
}
88