UrlFunctionTrait::to()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 19
rs 9.9666
c 1
b 0
f 1
ccs 0
cts 10
cp 0
crap 6
1
<?php
2
3
namespace Nip\Router\Generator\Traits;
4
5
use Nip\Router\RequestContext;
6
use Nip\Utility\Str;
7
8
/**
9
 * Trait UrlFunctionTrait
10
 * @package Nip\Router\Generator\Traits
11
 * @method RequestContext getContext()
12
 */
13
trait UrlFunctionTrait
14
{
15
    /**
16
     * Get the full URL for the current request.
17
     *
18
     * @return string
19
     */
20
    public function full()
21
    {
22
        return $this->getContext()->fullUrl();
23
    }
24
25
    /**
26
     * Get the current URL for the request.
27
     *
28
     * @return string
29
     */
30
    public function current()
31
    {
32
        return $this->to($this->getContext()->getPathInfo());
33
    }
34
35
    /**
36
     * Generate an absolute URL to the given path.
37
     *
38
     * @param  string  $path
39
     * @param  mixed  $extra
40
     * @param  bool|null  $secure
41
     * @return string
42
     */
43
    public function to($path, $extra = [], $secure = null)
44
    {
45
        // First we will check if the URL is already a valid URL. If it is we will not
46
        // try to generate a new one but will simply return the URL as is, which is
47
        // convenient since developers do not always have to check if it's valid.
48
        if ($this->isValidUrl($path)) {
49
            return $path;
50
        }
51
        $tail = implode('/', array_map(
52
                'rawurlencode', (array) $this->formatParameters($extra))
0 ignored issues
show
Bug introduced by
It seems like formatParameters() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
                'rawurlencode', (array) $this->/** @scrutinizer ignore-call */ formatParameters($extra))
Loading history...
53
        );
54
        // Once we have the scheme we will compile the "tail" by collapsing the values
55
        // into a single string delimited by slashes. This just makes it convenient
56
        // for passing the array of parameters to this URL as a list of segments.
57
        $root = $this->formatRoot($this->formatScheme($secure));
0 ignored issues
show
Bug introduced by
It seems like formatScheme() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        $root = $this->formatRoot($this->/** @scrutinizer ignore-call */ formatScheme($secure));
Loading history...
Bug introduced by
It seems like formatRoot() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $root = $this->formatRoot($this->formatScheme($secure));
Loading history...
58
        list($path, $query) = $this->extractQueryString($path);
59
        return $this->format(
0 ignored issues
show
Bug introduced by
It seems like format() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        return $this->/** @scrutinizer ignore-call */ format(
Loading history...
60
                $root, '/' . trim($path . '/' . $tail, '/')
61
            ) . $query;
62
    }
63
64
    /**
65
     * Generate the URL to an application asset.
66
     *
67
     * @param  string $path
68
     * @param  bool|null $secure
69
     * @return string
70
     */
71
    public function asset($path, $secure = null)
72
    {
73
        if ($this->isValidUrl($path)) {
74
            return $path;
75
        }
76
        // Once we get the root URL, we will check to see if it contains an index.php
77
        // file in the paths. If it does, we will remove it since it is not needed
78
        // for asset paths, but only for routes to endpoints in the application.
79
        $root = $this->formatRoot($this->formatScheme($secure));
80
        return $this->removeIndex($root) . '/assets/' . trim($path, '/');
0 ignored issues
show
Bug introduced by
It seems like removeIndex() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        return $this->/** @scrutinizer ignore-call */ removeIndex($root) . '/assets/' . trim($path, '/');
Loading history...
81
    }
82
83
    /**
84
     * Determine if the given path is a valid URL.
85
     *
86
     * @param  string $path
87
     * @return bool
88
     */
89
    public function isValidUrl($path)
90
    {
91
        if (!Str::startsWith($path, ['#', '//', 'mailto:', 'tel:', 'http://', 'https://'])) {
92
            return filter_var($path, FILTER_VALIDATE_URL) !== false;
93
        }
94
        return true;
95
    }
96
97
98
    /**
99
     * Extract the query string from the given path.
100
     *
101
     * @param  string  $path
102
     * @return string[]
103
     */
104
    protected function extractQueryString($path)
105
    {
106
        if (($queryPosition = strpos($path, '?')) !== false) {
107
            return [
108
                substr($path, 0, $queryPosition),
109
                substr($path, $queryPosition),
110
            ];
111
        }
112
        return [$path, ''];
113
    }
114
}
115