UnlinkController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 12
dl 61
loc 61
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 19 19 3
B defaultAction() 34 34 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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