LinkTranslator::translateParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 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\Identity,
11
    Link\Parameter
12
};
13
use Innmind\Http\Header\{
14
    Link,
15
    LinkValue,
16
    Parameter as HttpParameter
17
};
18
use Innmind\Immutable\{
19
    MapInterface,
20
    Map
21
};
22
use Symfony\Component\Routing\RouterInterface;
23
24
final class LinkTranslator
25
{
26
    private $locate;
27
    private $router;
28
29 16
    public function __construct(Locator $locator, RouterInterface $router)
30
    {
31 16
        $this->locate = $locator;
32 16
        $this->router = $router;
33 16
    }
34
35
    /**
36
     * @return MapInterface<Reference, MapInterface<string, Parameter>>
37
     */
38 8
    public function translate(Link $link): MapInterface
39
    {
40
        return $link
41 8
            ->values()
42 8
            ->reduce(
43 8
                new Map(Reference::class, MapInterface::class),
44 8
                function(Map $carry, LinkValue $link): Map {
45 8
                    list($reference, $parameters) = $this->translateLinkValue($link);
46
47 6
                    return $carry->put($reference, $parameters);
48 8
                }
49
            );
50
    }
51
52
    /**
53
     * @return array<Reference, MapInterface<string, Parameter>>
0 ignored issues
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...
54
     */
55 8
    private function translateLinkValue(LinkValue $link): array
56
    {
57 8
        $infos = $this->router->match((string) $link->url());
58
59
        if (
60 8
            !isset($infos['_innmind_resource']) ||
61 8
            !isset($infos['identity'])
62
        ) {
63 2
            throw new UnexpectedValueException;
64
        }
65
66
        return [
67 6
            new Reference(
68 6
                ($this->locate)($infos['_innmind_resource']),
69 6
                new Identity($infos['identity'])
70
            ),
71
            $this
72 6
                ->translateParameters($link->parameters())
73 6
                ->put('rel', new Parameter\Parameter('rel', $link->relationship()))
74
        ];
75
    }
76
77
    /**
78
     * @return MapInterface<string, Parameter>
0 ignored issues
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...
79
     */
80 6
    private function translateParameters(MapInterface $parameters): MapInterface
81
    {
82 6
        return $parameters->reduce(
83 6
            new Map('string', Parameter::class),
84 6
            function(Map $carry, string $name, HttpParameter $param): Map {
85 2
                return $carry->put(
86 2
                    $name,
87 2
                    new Parameter\Parameter($name, $param->value())
88
                );
89 6
            }
90
        );
91
    }
92
}
93