Completed
Push — develop ( b8891a...ffebe5 )
by Baptiste
04:15
created

UnlinkController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 11
dl 53
loc 53
ccs 4
cts 24
cp 0.1666
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 3
B defaultAction() 29 29 1

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\{
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 1
    public function __construct(
42
        MapInterface $gateways,
43
        UnlinkBuilderInterface $headerBuilder,
44
        LinkTranslator $translator
45
    ) {
46
        if (
47 1
            (string) $gateways->keyType() !== 'string' ||
48 1
            (string) $gateways->valueType() !== GatewayInterface::class
49
        ) {
50 1
            throw new InvalidArgumentException;
51
        }
52
53
        $this->gateways = $gateways;
54
        $this->headerBuilder = $headerBuilder;
55
        $this->translator = $translator;
56
    }
57
58
    public function defaultAction(Request $request, $identity): ResponseInterface
59
    {
60
        $from = $request->attributes->get('_innmind_resource_definition');
61
        $request = $request->attributes->get('_innmind_request');
62
        $tos = $this->translator->translate($request->headers()->get('Link'));
63
64
        $linker = $this
65
            ->gateways
66
            ->get((string) $from->gateway())
67
            ->resourceLinker();
68
        $linker(
69
            $from = new Reference($from, new Identity($identity)),
70
            $tos
71
        );
72
73
        return new Response(
74
            $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')),
75
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
76
            $request->protocolVersion(),
77
            new Headers(
78
                $this->headerBuilder->build(
79
                    $request,
80
                    $from,
81
                    $tos
82
                )
83
            ),
84
            new StringStream('')
85
        );
86
    }
87
}
88