Completed
Push — develop ( 99d87a...0261a6 )
by Baptiste
03:07
created

ListController::defaultAction()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 66
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6.0106

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 28
cts 30
cp 0.9333
rs 8.6045
c 0
b 0
f 0
cc 6
eloc 45
nc 8
nop 1
crap 6.0106

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    RangeExtractor\ExtractorInterface,
12
    SpecificationBuilder\BuilderInterface,
13
    Response\HeaderBuilder\ListBuilderInterface,
14
    GatewayInterface,
15
    Request\Range,
16
    Exception\RangeNotFoundException,
17
    Exception\NoFilterFoundException
18
};
19
use Innmind\Http\{
20
    Message\ResponseInterface,
21
    Message\Response,
22
    Message\StatusCode,
23
    Message\ReasonPhrase,
24
    Headers,
25
    Exception\Http\RangeNotSatisfiableException
26
};
27
use Innmind\Filesystem\Stream\StringStream;
28
use Innmind\Immutable\MapInterface;
29
use Symfony\Component\{
30
    HttpFoundation\Request,
31
    Serializer\SerializerInterface
32
};
33
34
final class ListController
35
{
36
    private $format;
37
    private $serializer;
38
    private $rangeExtractor;
39
    private $specificationBuilder;
40
    private $gateways;
41
    private $headerBuilder;
42
43 3
    public function __construct(
44
        Format $format,
45
        SerializerInterface $serializer,
46
        ExtractorInterface $rangeExtractor,
47
        BuilderInterface $specificationBuilder,
48
        MapInterface $gateways,
49
        ListBuilderInterface $headerBuilder
50
    ) {
51
        if (
52 3
            (string) $gateways->keyType() !== 'string' ||
53 3
            (string) $gateways->valueType() !== GatewayInterface::class
54
        ) {
55
            throw new InvalidArgumentException;
56
        }
57
58 3
        $this->format = $format;
59 3
        $this->serializer = $serializer;
60 3
        $this->rangeExtractor = $rangeExtractor;
61 3
        $this->specificationBuilder = $specificationBuilder;
62 3
        $this->gateways = $gateways;
63 3
        $this->headerBuilder = $headerBuilder;
64 3
    }
65
66 3
    public function defaultAction(Request $request): ResponseInterface
67
    {
68 3
        $definition = $request->attributes->get('_innmind_resource_definition');
69 3
        $request = $request->attributes->get('_innmind_request');
70
71
        try {
72 3
            $range = $this->rangeExtractor->extract($request);
73 1
        } catch (RangeNotFoundException $e) {
74 1
            $range = null;
75
        }
76
77
        try {
78 3
            $specification = $this->specificationBuilder->buildFrom(
79
                $request,
80
                $definition
81
            );
82
        } catch (NoFilterFoundException $e) {
83
            $specification = null;
84
        }
85
86
        $accessor = $this
87 3
            ->gateways
88 3
            ->get((string) $definition->gateway())
89 3
            ->resourceListAccessor();
90 3
        $identities = $accessor(
91
            $definition,
92
            $specification,
93
            $range
94
        );
95
96
        if (
97 3
            $identities->size() === 0 &&
98 3
            $range instanceof Range
99
        ) {
100 1
            throw new RangeNotSatisfiableException;
101
        }
102
103 2
        return new Response(
104 2
            $code = new StatusCode(StatusCode::codes()->get(
105 2
                $range instanceof Range ? 'PARTIAL_CONTENT' : 'OK'
106
            )),
107 2
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
108 2
            $request->protocolVersion(),
109 2
            new Headers(
110 2
                $this->headerBuilder->build(
111
                    $identities,
112
                    $request,
113
                    $definition,
114
                    $specification,
115
                    $range
116
                )
117
            ),
118 2
            new StringStream(
119 2
                $this->serializer->serialize(
120
                    $identities,
121 2
                    $this->format->acceptable($request)->name(),
122
                    [
123 2
                        'request' => $request,
124 2
                        'definition' => $definition,
125 2
                        'specification' => $specification,
126 2
                        'range' => $range,
127
                    ]
128
                )
129
            )
130
        );
131
    }
132
}
133