Completed
Push — develop ( dd6a23...99c8cd )
by Baptiste
06:26
created

CapabilitiesController::capabilitiesAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 32
cts 32
cp 1
rs 9.6716
c 0
b 0
f 0
cc 2
eloc 35
nc 2
nop 1
crap 2

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;
5
6
use Innmind\Rest\ServerBundle\{
7
    Routing\RouteFactory,
8
    Exception\InvalidArgumentException
9
};
10
use Innmind\Rest\Server\{
11
    Definition\Directory,
12
    Action
13
};
14
use Innmind\Http\{
15
    Message\ServerRequestInterface,
16
    Message\ResponseInterface,
17
    Message\Response,
18
    Message\StatusCode,
19
    Message\ReasonPhrase,
20
    Header\HeaderInterface,
21
    Header\LinkValue,
22
    Header\Link,
23
    Header\ParameterInterface,
24
    Header\HeaderValueInterface,
25
    Headers
26
};
27
use Innmind\Url\Url;
28
use Innmind\Filesystem\Stream\StringStream;
29
use Innmind\Immutable\{
30
    Map,
31
    Set,
32
    MapInterface
33
};
34
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
35
36
final class CapabilitiesController
37
{
38
    private $directories;
39
    private $routeFactory;
40
    private $generator;
41
42 2
    public function __construct(
43
        MapInterface $directories,
44
        RouteFactory $routeFactory,
45
        UrlGeneratorInterface $generator
46
    ) {
47
        if (
48 2
            (string) $directories->keyType() !== 'string' ||
49 2
            (string) $directories->valueType() !== Directory::class
50
        ) {
51 1
            throw new InvalidArgumentException;
52
        }
53
54 1
        $this->directories = $directories;
55 1
        $this->routeFactory = $routeFactory;
56 1
        $this->generator = $generator;
57 1
    }
58
59 1
    public function capabilitiesAction(ServerRequestInterface $request): ResponseInterface
60
    {
61
        $links = $this
62 1
            ->directories
63 1
            ->reduce(
64 1
                [],
65
                function(array $carry, string $name, Directory $directory) {
66 1
                    return array_merge(
67
                        $carry,
68
                        $directory
69 1
                            ->flatten()
70 1
                            ->reduce(
71 1
                                [],
72 1
                                function(array $carry, string $name) {
73 1
                                    $carry[$name] = $this->generator->generate(
74
                                        $this
75 1
                                            ->routeFactory
76 1
                                            ->makeRoute(
77
                                                $name,
78 1
                                                new Action(Action::OPTIONS)
79
                                            )
80 1
                                            ->getPath()
81
                                    );
82
83 1
                                    return $carry;
84 1
                                }
85
                            )
86
                    );
87 1
                }
88
            );
89 1
        $set = new Set(HeaderValueInterface::class);
90
91 1
        foreach ($links as $name => $link) {
92 1
            $set = $set->add(new LinkValue(
93 1
                Url::fromString($link),
94
                $name,
95 1
                new Map('string', ParameterInterface::class)
96
            ));
97
        }
98
99 1
        return new Response(
100 1
            $code = new StatusCode(StatusCode::codes()->get('OK')),
101 1
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
102 1
            $request->protocolVersion(),
103 1
            new Headers(
104 1
                (new Map('string', HeaderInterface::class))
105 1
                    ->put(
106 1
                        'Link',
107 1
                        new Link($set)
108
                    )
109
            ),
110 1
            new StringStream('')
111
        );
112
    }
113
}
114