Test Failed
Push — master ( 73c405...d57cd2 )
by Konstantins
03:14
created

UrlGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Routing;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\UriInterface;
7
use Venta\Contracts\Routing\ImmutableRouteCollection as RouteCollectionContract;
8
use Venta\Contracts\Routing\Route as RouteContract;
9
use Venta\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
10
use Venta\Routing\Exception\RouteNotFoundException;
11
12
/**
13
 * Class UrlGenerator
14
 *
15
 * @package Venta\Routing
16
 */
17
class UrlGenerator implements UrlGeneratorContract
18
{
19
    /**
20
     * @var ServerRequestInterface
21
     */
22
    private $request;
23
24
    /**
25
     * @var RouteCollectionContract
26
     */
27
    private $routes;
28
29
    /**
30
     * @var UriInterface
31
     */
32
    private $uri;
33
34
    /**
35
     * UrlGenerator constructor.
36
     *
37
     * @param ServerRequestInterface $request
38
     * @param RouteCollectionContract $routes
39
     * @param UriInterface $uri
40
     */
41 3
    public function __construct(ServerRequestInterface $request, RouteCollectionContract $routes, UriInterface $uri)
42
    {
43 3
        $this->request = $request;
44 3
        $this->routes = $routes;
45 3
        $this->uri = $uri;
46 3
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 1 View Code Duplication
    public function toCurrent(array $variables = [], array $query = []): UriInterface
52
    {
53 1
        $route = $this->request->getAttribute('route');
54
55 1
        if ($route === null) {
56
            throw new RouteNotFoundException(
57
                sprintf('Unable to generate an URL for current.')
58
            );
59
        }
60
61 1
        return $this->buildRouteUri($route, $variables, $query);
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 1 View Code Duplication
    public function toRoute(string $routeName, array $variables = [], array $query = []): UriInterface
68
    {
69 1
        $route = $this->routes->findByName($routeName);
70
71 1
        if ($route === null) {
72
            throw new RouteNotFoundException(
73
                sprintf('Unable to generate an URL for the named route "%s" as such route does not exist.', $routeName)
74
            );
75
        }
76
77 1
        return $this->buildRouteUri($route, $variables, $query);
78
    }
79
80
    /**
81
     * Builds URI for provided route instance.
82
     *
83
     * @param RouteContract $route
84
     * @param array $variables
85
     * @param array $query
86
     * @return UriInterface
87
     */
88 2
    private function buildRouteUri(RouteContract $route, array $variables = [], array $query = []): UriInterface
89
    {
90 2
        $uri = $this->uri
91 2
            ->withScheme($route->scheme() ?: $this->request->getUri()->getScheme())
92 2
            ->withHost($route->host() ?: $this->request->getUri()->getHost())
93 2
            ->withPath($route->compilePath($variables));
94
95
        // Check if we need to add current request port to the uri.
96 2
        $requestPort = $this->request->getUri()->getPort();
97 2
        if (!in_array($requestPort, [80, 443])) {
98 1
            $uri = $uri->withPort($requestPort);
99
        }
100
101 2
        if ($query) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
102 2
            $uri = $uri->withQuery(http_build_query($query));
103
        }
104
105 2
        return $uri;
106
    }
107
}