Test Failed
Push — develop ( ff58ad...b8f9b2 )
by steve
13:44 queued 12s
created

Url::goBack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
ccs 0
cts 5
cp 0
crap 6
1
<?php
2
3
namespace neon\core\helpers;
4
5
use neon\core\helpers\Arr;
6
use yii\base\InvalidConfigException;
7
8
class Url extends \yii\helpers\Url
9
{
10
	/**
11
	 * This returns the previous page referrer to go back.
12
	 * If there is not a referring page, then it will default to return the url specified
13
	 *
14
	 * @param mixed $url
15
	 * @see self::to()
16
	 * @return string url
17
	 */
18
	public static function goBack($url = '')
19
	{
20
		$back = neon()->request->referrer;
21
		if ($back) {
22
			return $back;
23
		}
24
		return self::to($url);
25
	}
26
27
	/**
28
	 * Apply the base url to the passed in $url
29
	 *
30
	 * @param string $url
31
	 * @param bool $scheme - whether to add the http / https scheme
32
	 * @return string
33
	 */
34
	public static function baseUrl($url, $scheme=false)
35
	{
36
		return self::base($scheme) . '/' . ltrim($url, '/');
37
	}
38
39
	/**
40
	 * @deprecated!  THIS IS CMS SPECIFIC! OOH ERR
41
	 * @param type $pageId
0 ignored issues
show
Bug introduced by
The type neon\core\helpers\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
	 * @param type $scheme
43
	 * @return type
44
	 */
45
	public static function getPageUrl($pageId, $scheme=false)
46
	{
47
		return self::base($scheme) . neon('cms')->page->getUrlOfPage($pageId);
0 ignored issues
show
Bug Best Practice introduced by
The property page does not exist on neon\core\ApplicationWeb. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getUrlOfPage() does not exist on null. ( Ignorable by Annotation )

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

47
		return self::base($scheme) . neon('cms')->page->/** @scrutinizer ignore-call */ getUrlOfPage($pageId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
	}
49
50
	/**
51
	 * Matches a route to see if it is the current route.
52
	 * the specificity of the match is based on the passed in route for example:
53
	 * - if $route is: 'user' then this function will return true for all controllers and actions within the user module.
54
	 * - if $route is: 'user/admin' this will return true for all actions under the user module's admin controller.
55
	 * - if $route is: 'user/admin/account' this will only return true for the account action inside the admin controller of the user modules.
56
	 *
57
	 * @param string $route ModuleId / ControllerId / ActionId
58
	 * @return boolean
59
	 * @deprected use neon()->request->isRoute(); which copes with unlimited nesting and wilcard patterns as well as param matching
60
	 */
61
	public static function isRoute($route, $currentUrl=false)
62
	{
63
		// deprecated('\neon\core\helpers\Url::isRoute', 'Please use neon()->request->isRoute() which copes with unlimited nesting and wilcard patterns as well as param matching');
64
		$r = explode('/', trim($route, '/'));
65
		$currentUrl ? $currentUrl : \Neon::$app->controller->getRoute();
66
		$currentRoute = explode('/', trim($currentUrl,'/'));
67
68
		$m = Arr::getValue($currentRoute, 0); // module
69
		$c = Arr::getValue($currentRoute, 1); // controller
70
		$a = Arr::getValue($currentRoute, 2); // action
71
72
		switch(count($r)){
73
			case 1:
74
				// match just module
75
				return $r[0] == $m;
76
			case 2:
77
				// match the module and the controller
78
				return ($r[0] == $m && $r[1] == $c);
79
			case 3:
80
				// match the module / controller /action
81
				return ($r[0] == $m && $r[1] == $c && $r[2] == $a) ;
82
		}
83
		return false;
84
	}
85
86
	/**
87
	 * Matches the url path (not including the web base url (the result of Url::base() ))
88
	 * must have an exact match to return true
89
	 * ***NOTE*** This does not account for url rewrites! It looks at the current url only and matches against the string
90
	 * For partial matches use neon()->request->is('my/path*');
91
	 * For example a web url of ```http://newicon.net/thing/neon/code``` with a Url::base of http://newicon.net/thing
92
	 * will match exactly if a $url parameter of ```/neon/code``` is passed in.
93
	 *
94
	 * Note: can accept multiple urls - will return true if any one urls matches the current url
95
	 * @example
96
	 * ```php
97
	 * // current url === /two
98
	 * isUrl('/one', '/two');
99
	 * // => true
100
	 * ```
101
	 *
102
	 * @param string|array $urls,... accepts multiple parameters representing urls to check - will return true if any match
103
	 * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration
104
	 * @return boolean
105
	 */
106
	public static function isUrl(...$urls)
107
	{
108
		// if an array is passed in it will get nested one level because of the spread operator
109
		if (is_array($urls[0])) $urls = $urls[0];
110
		$currentUrl = neon()->request->getPathInfo();
111
		foreach($urls as $url) {
112
			$url = str_replace(Url::base(), '', $url);
113
			if ($currentUrl !== '' && ltrim($url, '/') === $currentUrl) {
114
				return true;
115
			}
116
		}
117
118
		return false;
119
	}
120
121
	/**
122
	 * Whether a url is absolute starts with // or :// or starts with 'data:'
123
	 *
124
	 * @param $url
125
	 * @return bool
126
	 */
127
	public static function isAbsolute($url)
128
	{
129
		$absolute = !static::isRelative($url);
130
		$isDataUrl = (substr($url, 0, 5) === 'data:');
131
		return $isDataUrl || $absolute;
132
	}
133
134
	/**
135
	 * Returns the canonical URL of the currently requested page.
136
	 *
137
	 * The canonical URL is constructed using the current controller's [[\yii\web\Controller::route]] and
138
	 * [[\yii\web\Controller::actionParams]]. You may use the following code in the layout view to add a link tag
139
	 * about canonical URL:
140
	 *
141
	 * ```php
142
	 * $this->registerLinkTag(['rel' => 'canonical', 'href' => Url::canonical()]);
143
	 * ```
144
	 *
145
	 * @return string the canonical URL of the currently requested page
146
	 */
147
	public static function canonical()
148
	{
149
		return neon()->urlManager->getCanonical();
150
	}
151
}