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

GetController::defaultAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 20
cts 20
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 2
crap 1
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