UnlinkController::defaultAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 23

Duplication

Lines 34
Ratio 100 %

Code Coverage

Tests 22
CRAP Score 2

Importance

Changes 0
Metric Value
dl 34
loc 34
ccs 22
cts 22
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Controller\Resource;
5
6
use Innmind\Rest\ServerBundle\Translator\LinkTranslator;
7
use Innmind\Rest\Server\{
8
    Response\HeaderBuilder\UnlinkBuilder,
9
    Gateway,
10
    Reference,
11
    Identity\Identity
12
};
13
use Innmind\Http\{
14
    Message\Response,
15
    Message\StatusCode\StatusCode,
16
    Message\ReasonPhrase\ReasonPhrase,
17
    Headers\Headers,
18
    Exception\Http\BadRequest
19
};
20
use Innmind\Filesystem\Stream\StringStream;
21
use Innmind\Immutable\{
22
    MapInterface,
23
    Map
24
};
25
use Symfony\Component\HttpFoundation\Request;
26
27 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...
28
{
29
    private $gateways;
30
    private $buildHeader;
31
    private $translator;
32
33 6
    public function __construct(
34
        MapInterface $gateways,
35
        UnlinkBuilder $headerBuilder,
36
        LinkTranslator $translator
37
    ) {
38
        if (
39 6
            (string) $gateways->keyType() !== 'string' ||
40 6
            (string) $gateways->valueType() !== Gateway::class
41
        ) {
42 2
            throw new \TypeError(sprintf(
43 2
                'Argument 1 must be of type MapInterface<string, %s>',
44 2
                Gateway::class
45
            ));
46
        }
47
48 4
        $this->gateways = $gateways;
49 4
        $this->buildHeader = $headerBuilder;
50 4
        $this->translator = $translator;
51 4
    }
52
53 4
    public function defaultAction(Request $request, $identity): Response
54
    {
55 4
        $from = $request->attributes->get('_innmind_resource_definition');
56 4
        $request = $request->attributes->get('_innmind_request');
57
58 4
        if (!$request->headers()->has('Link')) {
59 2
            throw new BadRequest;
60
        }
61
62 2
        $tos = $this->translator->translate($request->headers()->get('Link'));
63
64
        $unlinker = $this
65 2
            ->gateways
66 2
            ->get((string) $from->gateway())
67 2
            ->resourceUnlinker();
68 2
        $unlinker(
69 2
            $from = new Reference($from, new Identity($identity)),
70 2
            $tos
71
        );
72
73 2
        return new Response\Response(
74 2
            $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')),
75 2
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
76 2
            $request->protocolVersion(),
77 2
            new Headers(
78 2
                ($this->buildHeader)(
79 2
                    $request,
80 2
                    $from,
81 2
                    $tos
82
                )
83
            ),
84 2
            new StringStream('')
85
        );
86
    }
87
}
88