CapabilitiesController   B
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 17
dl 0
loc 78
ccs 35
cts 35
cp 1
rs 7.8571
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A capabilitiesAction() 0 51 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Controller;
5
6
use Innmind\Rest\ServerBundle\Routing\RouteFactory;
7
use Innmind\Rest\Server\{
8
    Definition\Directory,
9
    Action
10
};
11
use Innmind\Http\{
12
    Message\Response,
13
    Message\StatusCode\StatusCode,
14
    Message\ReasonPhrase\ReasonPhrase,
15
    Header,
16
    Header\LinkValue,
17
    Header\Link,
18
    Header\Value,
19
    Headers\Headers
20
};
21
use Innmind\Url\Url;
22
use Innmind\Filesystem\Stream\StringStream;
23
use Innmind\Immutable\{
24
    Set,
25
    MapInterface,
26
    Map
27
};
28
use Symfony\Component\{
29
    Routing\Generator\UrlGeneratorInterface,
30
    HttpFoundation\Request
31
};
32
33
final class CapabilitiesController
34
{
35
    private $directories;
36
    private $routeFactory;
37
    private $generator;
38
39 4
    public function __construct(
40
        MapInterface $directories,
41
        RouteFactory $routeFactory,
42
        UrlGeneratorInterface $generator
43
    ) {
44
        if (
45 4
            (string) $directories->keyType() !== 'string' ||
46 4
            (string) $directories->valueType() !== Directory::class
47
        ) {
48 2
            throw new \TypeError(sprintf(
49 2
                'Argument 1 must be of type MapInterface<string, %s>',
50 2
                Directory::class
51
            ));
52
        }
53
54 2
        $this->directories = $directories;
55 2
        $this->routeFactory = $routeFactory;
56 2
        $this->generator = $generator;
57 2
    }
58
59 2
    public function capabilitiesAction(Request $request): Response
60
    {
61 2
        $request = $request->attributes->get('_innmind_request');
62
63
        $links = $this
64 2
            ->directories
65 2
            ->reduce(
66 2
                new Map('string', 'string'),
67 2
                function(Map $carry, string $name, Directory $directory): Map {
68
                    return $directory
69 2
                        ->flatten()
70 2
                        ->reduce(
71 2
                            $carry,
72 2
                            function(Map $carry, string $name): Map {
73 2
                                $route = $this->routeFactory->makeName(
74 2
                                    $name,
75 2
                                    Action::options()
76
                                );
77
78 2
                                return $carry->put(
79 2
                                    $name,
80 2
                                    $this->generator->generate($route)
81
                                );
82 2
                            }
83
                        );
84 2
                }
85
            )
86 2
            ->reduce(
87 2
                new Set(Value::class),
88 2
                static function(Set $carry, string $name, string $link): Set {
89 2
                    return $carry->add(new LinkValue(
90 2
                        Url::fromString($link),
91 2
                        $name
92
                    ));
93 2
                }
94
            );
95
96
        return new Response\Response(
97
            $code = new StatusCode(StatusCode::codes()->get('OK')),
98
            new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())),
99
            $request->protocolVersion(),
100
            new Headers(
101
                (new Map('string', Header::class))
102
                    ->put(
103
                        'Link',
104
                        new Link(...$links)
105
                    )
106
            ),
107
            new StringStream('')
108
        );
109
    }
110
}
111