Completed
Push — develop ( b8891a...ffebe5 )
by Baptiste
04:15
created

LinkTranslator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 9
dl 0
loc 69
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A translate() 0 13 1
A translateLinkValue() 0 21 3
A translateParameters() 0 12 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Translator;
5
6
use Innmind\Rest\ServerBundle\Exception\UnexpectedValueException;
7
use Innmind\Rest\Server\{
8
    Definition\Locator,
9
    Reference,
10
    Identity,
11
    Link\ParameterInterface,
12
    Link\Parameter
13
};
14
use Innmind\Http\Header\{
15
    Link,
16
    LinkValue,
17
    ParameterInterface as LinkParameterInterface
18
};
19
use Innmind\Immutable\{
20
    MapInterface,
21
    Map
22
};
23
use Symfony\Component\Routing\RouterInterface;
24
25
final class LinkTranslator
26
{
27
    private $locator;
28
    private $router;
29
30 6
    public function __construct(Locator $locator, RouterInterface $router)
31
    {
32 6
        $this->locator = $locator;
33 6
        $this->router = $router;
34 6
    }
35
36
    /**
37
     * @return MapInterface<Reference, MapInterface<string, ParameterInterface>>
1 ignored issue
show
Documentation introduced by
The doc-type MapInterface<Reference, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
38
     */
39 4
    public function translate(Link $link): MapInterface
40
    {
41
        return $link
42 4
            ->values()
43 4
            ->reduce(
44 4
                new Map(Reference::class, MapInterface::class),
45
                function(Map $carry, LinkValue $link): Map {
46 4
                    list($reference, $parameters) = $this->translateLinkValue($link);
47
48 3
                    return $carry->put($reference, $parameters);
49 4
                }
50
            );
51
    }
52
53
    /**
54
     * @return array<Reference, MapInterface<string, ParameterInterface>>
1 ignored issue
show
Documentation introduced by
The doc-type array<Reference, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
55
     */
56 4
    private function translateLinkValue(LinkValue $link): array
57
    {
58 4
        $infos = $this->router->match((string) $link->url());
59
60
        if (
61 4
            !isset($infos['_innmind_resource']) ||
62 4
            !isset($infos['identity'])
63
        ) {
64 1
            throw new UnexpectedValueException;
65
        }
66
67
        return [
68 3
            new Reference(
69 3
                $this->locator->locate($infos['_innmind_resource']),
70 3
                new Identity($infos['identity'])
71
            ),
72
            $this
73 3
                ->translateParameters($link->parameters())
74 3
                ->put('rel', new Parameter('rel', $link->relationship()))
75
        ];
76
    }
77
78
    /**
79
     * @return MapInterface<string, ParameterInterface>
1 ignored issue
show
Documentation introduced by
The doc-type MapInterface<string, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81 3
    private function translateParameters(MapInterface $parameters): MapInterface
82
    {
83 3
        return $parameters->reduce(
84 3
            new Map('string', ParameterInterface::class),
85 3
            function(Map $carry, string $name, LinkParameterInterface $param): Map {
86 1
                return $carry->put(
87
                    $name,
88 1
                    new Parameter($name, $param->value())
89
                );
90 3
            }
91
        );
92
    }
93
}
94