LinkController::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
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\Translator\LinkTranslator;
7
use Innmind\Rest\Server\{
8
    Response\HeaderBuilder\LinkBuilder,
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 LinkController
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
        LinkBuilder $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
        $linker = $this
65 2
            ->gateways
66 2
            ->get((string) $from->gateway())
67 2
            ->resourceLinker();
68 2
        $linker(
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)($request, $from, $tos)
79
            ),
80 2
            new StringStream('')
81
        );
82
    }
83
}
84