Completed
Push — master ( 7b2ae7...b60607 )
by Dominik
06:56
created

LinkGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiSkeleton\Serialization;
6
7
use Chubbyphp\Serialization\Link\Link;
8
use Chubbyphp\Serialization\Link\LinkGeneratorInterface;
9
use Chubbyphp\Serialization\Link\LinkInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Slim\Interfaces\RouterInterface;
12
use Slim\Route;
13
14
final class LinkGenerator implements LinkGeneratorInterface
15
{
16
    /**
17
     * @var RouterInterface
18
     */
19
    private $router;
20
21
    /**
22
     * @param RouterInterface $router
23
     */
24 1
    public function __construct(RouterInterface $router)
25
    {
26 1
        $this->router = $router;
27 1
    }
28
29
    /**
30
     * @param string $routeName
31
     * @param array  $data
32
     * @param array  $queryParams
33
     *
34
     * @return LinkInterface
35
     */
36 1
    public function generateLink(
37
        Request $request,
38
        string $routeName,
39
        array $data = [],
40
        array $queryParams = []
41
    ): LinkInterface {
42
        /** @var Route $route */
43 1
        $route = $this->router->getNamedRoute($routeName);
44
45 1
        return new Link(
46 1
            $this->router->pathFor($routeName, $data, $queryParams),
47 1
            implode('|', $route->getMethods())
48
        );
49
    }
50
}
51