Completed
Push — develop ( 87c85a...e7e450 )
by Baptiste
03:28
created

GetController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 14
dl 18
loc 66
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B defaultAction() 0 39 1
A __construct() 18 18 3

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
    Format,
8
    Exception\InvalidArgumentException
9
};
10
use Innmind\Rest\Server\{
11
    Response\HeaderBuilder\GetBuilderInterface,
12
    GatewayInterface,
13
    Identity
14
};
15
use Innmind\Http\{
16
    Message\ResponseInterface,
17
    Message\Response,
18
    Message\StatusCode,
19
    Message\ReasonPhrase,
20
    Headers
21
};
22
use Innmind\Filesystem\Stream\StringStream;
23
use Innmind\Immutable\MapInterface;
24
use Symfony\Component\{
25
    HttpFoundation\Request,
26
    Serializer\SerializerInterface
27
};
28
29
final class GetController
30
{
31
    private $format;
32
    private $serializer;
33
    private $gateways;
34
    private $headerBuilder;
35
36 2 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
37
        Format $format,
38
        SerializerInterface $serializer,
39
        MapInterface $gateways,
40
        GetBuilderInterface $headerBuilder
41
    ) {
42
        if (
43 2
            (string) $gateways->keyType() !== 'string' ||
44 2
            (string) $gateways->valueType() !== GatewayInterface::class
45
        ) {
46 1
            throw new InvalidArgumentException;
47
        }
48
49 1
        $this->format = $format;
50 1
        $this->serializer = $serializer;
51 1
        $this->gateways = $gateways;
52 1
        $this->headerBuilder = $headerBuilder;
53 1
    }
54
55 1
    public function defaultAction(Request $request, $identity): ResponseInterface
56
    {
57 1
        $definition = $request->attributes->get('_innmind_resource_definition');
58 1
        $request = $request->attributes->get('_innmind_request');
59
60
        $accessor = $this
61 1
            ->gateways
62 1
            ->get((string) $definition->gateway())
63 1
            ->resourceAccessor();
64 1
        $resource = $accessor(
65
            $definition,
66 1
            $identity = new Identity($identity)
67
        );
68
69 1
        return new Response(
70 1
            $code = new StatusCode(StatusCode::codes()->get('OK')),
71 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
72 1
            $request->protocolVersion(),
73 1
            new Headers(
74 1
                $this->headerBuilder->build(
75
                    $resource,
76
                    $request,
77
                    $definition,
78
                    $identity
79
                )
80
            ),
81 1
            new StringStream(
82 1
                $this->serializer->serialize(
83
                    $resource,
84 1
                    $this->format->acceptable($request)->name(),
85
                    [
86 1
                        'request' => $request,
87 1
                        'definition' => $definition,
88 1
                        'identity' => $identity,
89
                    ]
90
                )
91
            )
92
        );
93
    }
94
}
95