Completed
Push — develop ( 02617c...96603c )
by Baptiste
05:33
created

ListController::defaultAction()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 63
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 6.0048

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 37
cts 39
cp 0.9487
rs 8.6498
c 0
b 0
f 0
cc 6
eloc 43
nc 8
nop 1
crap 6.0048

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