Completed
Push — master ( 2c64ed...ead60c )
by Ruben
07:32
created

UrlResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resolve() 0 19 2
1
<?php
2
3
namespace Spatie\LaravelResourceEndpoints;
4
5
use Illuminate\Routing\Exceptions\UrlGenerationException;
6
use Illuminate\Routing\Route;
7
use Illuminate\Routing\RouteUrlGenerator;
8
use Illuminate\Routing\UrlGenerator;
9
10
class UrlResolver extends RouteUrlGenerator
11
{
12
    public function __construct(UrlGenerator $urlGenerator)
13
    {
14
        parent::__construct($urlGenerator, $urlGenerator->getRequest());
15
16
        // The URL defaults are not given through automatically
17
        $this->defaults($urlGenerator->getDefaultParameters());
18
    }
19
20
    public function resolve(Route $route, array $parameters): string
21
    {
22
        try {
23
            return $this->to(
24
                $route,
25
                $this->url->formatParameters($parameters),
26
                true
27
            );
28
        } catch (UrlGenerationException $exception) {
29
            // Create an uri with missing parameters between brackets
30
            $domain = $this->getRouteDomain($route, $parameters);
31
32
            return $this->addQueryString($this->url->format(
33
                $root = $this->replaceRootParameters($route, $domain, $parameters),
34
                $this->replaceRouteParameters($route->uri(), $parameters),
35
                $route
36
            ), $parameters);
37
        }
38
    }
39
}
40