Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

UrlFunctionTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A full() 0 4 1
A current() 0 4 1
A to() 0 20 2
A asset() 0 11 2
A isValidUrl() 0 7 2
A extractQueryString() 0 10 2
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))
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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));
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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
}