Completed
Push — master ( b015a1...0e1003 )
by Abdelrahman
49:45
created

UrlGenerator::routeUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Cortex Foundation Module.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Cortex Foundation Module
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
declare(strict_types=1);
17
18
namespace Cortex\Foundation\Overrides\Illuminate\Routing;
19
20
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator;
21
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
22
23
class UrlGenerator extends BaseUrlGenerator
24
{
25
    /**
26
     * The backend URI.
27
     *
28
     * @var string
29
     */
30
    protected $backendUri = 'backend';
31
32
    /**
33
     * Set the backend URI.
34
     *
35
     * @param string $backendUri
36
     *
37
     * @return void
38
     */
39
    public function setBackendUri($backendUri)
40
    {
41
        $this->backendUri = $backendUri;
42
    }
43
44
    /**
45
     * Generate an absolute URL to the given admin path.
46
     *
47
     * @param string $path
48
     * @param array  $parameters
49
     * @param bool   $secure
0 ignored issues
show
Documentation introduced by
Should the type for parameter $secure not be boolean|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
50
     *
51
     * @return string
52
     */
53
    public function toBackend($path, array $parameters = [], $secure = null)
54
    {
55
        return $this->to("{$this->backendUri}/{$path}", $parameters, $secure);
56
    }
57
58
    /**
59
     * Generate a absolute URL to the given path.
60
     *
61
     * @param string    $path
62
     * @param mixed     $extra
63
     * @param bool|null $secure
64
     *
65
     * @return string
66
     */
67
    public function to($path, $extra = [], $secure = null)
68
    {
69
        return config('rinvex.cortex.route.locale_prefix')
70
            ? LaravelLocalization::localizeURL(parent::to($path, $extra, $secure))
71
            : parent::to($path, $extra, $secure);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function routeUrl()
78
    {
79
        if (! $this->routeGenerator) {
80
            $this->routeGenerator = new RouteUrlGenerator($this, $this->request);
81
        }
82
83
        return $this->routeGenerator;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function previous($fallback = false)
90
    {
91
        return ($previousUrl = $this->request->input('previous_url')) ? $this->to($previousUrl) : parent::previous($fallback);
0 ignored issues
show
Bug introduced by
It seems like $previousUrl defined by $this->request->input('previous_url') on line 91 can also be of type array; however, Cortex\Foundation\Overri...ting\UrlGenerator::to() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function toRoute($route, $parameters, $absolute)
98
    {
99
        // Bind {locale} route parameter
100
        if (config('rinvex.cortex.route.locale_prefix') && ! isset($parameters['locale'])) {
101
            $parameters['locale'] = LaravelLocalization::getCurrentLocale();
102
        }
103
104
        return $this->routeUrl()->to(
105
            $route, $this->formatParameters($parameters), $absolute
106
        );
107
    }
108
}
109