Completed
Push — master ( ad16ce...5852b3 )
by Jacob
07:53
created

Router::get_router_name()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 37
Code Lines 31

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 37
rs 5.2653
cc 11
eloc 31
nc 11
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?
0 ignored issues
show
Security Best Practice introduced by
It is not recommend to use PHP's short opening tag <?, better use <?php, or <?= in case of outputting.

Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.

As a precaution to avoid these problems better use the long opening tag <?php.

Loading history...
2
3
Loader::load('utility', array(
4
	'Request',
5
	'URLDecode'));
6
7
abstract class Router
8
{
9
10
	public function __construct() {}
11
12
	public static function instance()
13
	{
14
		$router_name = self::get_router_name();
15
		$router = Loader::loadNew('router', $router_name);
16
		$router->route();
17
	}
18
19
	private static function get_router_name()
20
	{
21
		if(Request::isAJAX())
22
			return 'AJAXRouter';
23
		if(URLDecode::getURI() == '/sitemap.xml')
24
			return 'SitemapRouter';
25
		if(URLDecode::getURI() == '/rss/')
26
			return 'RSSRouter';
27
		
28
		switch(URLDecode::getSite())
29
		{
30
			case 'ajax' :
31
				return 'AjaxRouter';
32
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
33
			case 'blog' :
34
				return 'BlogRouter';
35
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
36
			case 'home' :
37
				return 'HomeRouter';
38
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
39
			case 'lifestream' :
40
				return 'LifestreamRouter';
41
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
42
			case 'portfolio' :
43
				return 'PortfolioRouter';
44
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
45
			case 'site' :
46
				return 'SiteRouter';
47
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
48
			case 'waterfalls' :
49
				return 'WaterfallRouter';
50
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
51
		}
52
		
53
		Debugger::logMessage("The router for " . URLDecode::getSite() . " was not loaded.");
54
		Loader::loadNew('controller', '/Error404Controller')->activate();
55
	}
56
57
	protected function route()
58
	{
59
		$uri = URLDecode::getURI();
60
		
61
		$this->check_for_redirect($uri);
62
		
63
		$controller = $this->get_controller($uri);
64
		Loader::loadNew('controller', $controller)
65
			->activate();
66
	}
67
68
	abstract protected function get_redirect_array();
69
	abstract protected function get_direct_array();
70
71
	final protected function check_for_redirect($redirect_uri)
72
	{
73
		foreach($this->get_redirect_array() as $check)
74
		{
75
			$redirect_uri = preg_replace($check->pattern, $check->replace, $redirect_uri);
76
		}
77
		
78
		$redirect_uri = $this->check_for_special_redirect($redirect_uri);
79
		
80
		if($this->requires_trailing_slash() && substr($redirect_uri, -1) != '/')
81
			$redirect_uri .= '/';
82
		
83
        if (URLDecode::getHost() == 'waterfalls.jacobemerick.com') {
84
            $redirect_uri = 'http://' . (!Loader::isLive() ? 'dev' : 'www') . '.waterfallsofthekeweenaw.com' . $redirect_uri;
85
        }
86
        
87
		if($redirect_uri == URLDecode::getURI())
88
			return;
89
		
90
		$controller_check = $redirect_uri;
91
		if(substr($redirect_uri, 0, 4) == 'http')
92
			$controller_check = preg_replace('@^http://([a-z\.]+)@', '', $redirect_uri);
93
		
94
		$controller = $this->get_controller($controller_check);
95
		if($controller == '/Error404Controller')
96
		{
97
			Loader::loadNew('controller', '/Error404Controller')
98
				->activate();
99
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method check_for_redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
100
		}
101
102
		if(substr($redirect_uri, 0, 4) != 'http')
103
		{
104
			$redirect_uri = substr($redirect_uri, 1);
105
			$redirect_uri = URLDecode::getBase() . $redirect_uri;
106
		}
107
		
108
		Loader::loadNew('controller', '/Error301Controller', (array) $redirect_uri)
109
			->activate();
110
	}
111
112
	protected function check_for_special_redirect($uri)
113
	{
114
		return $uri;
115
	}
116
117
	final private function get_controller($uri)
118
	{
119
		foreach($this->get_direct_array() as $check)
120
		{
121
			if($uri == $check->match)
122
				return "{$this->get_primary_folder()}/{$check->controller}";
123
			
124
			if(preg_match("@^{$check->match}$@", $uri))
125
				return "{$this->get_primary_folder()}/{$check->controller}";
126
		}
127
		
128
		return '/Error404Controller';
129
	}
130
131
	final private function get_primary_folder()
132
	{
133
		if(Request::isAjax())
134
			return 'ajax';
135
		if(URLDecode::getURI() == '/sitemap.xml')
136
			return 'sitemap';
137
		if(URLDecode::getURI() == '/rss/')
138
			return 'rss';
139
		
140
		return URLDecode::getSite();
141
	}
142
143
	private function requires_trailing_slash()
144
	{
145
		return (
146
			URLDecode::getURI() != '/sitemap.xml' &&
147
			URLDecode::getExtension() != 'json' &&
148
			URLDecode::getExtension() != 'jpg' &&
149
			URLDecode::getExtension() != 'png' &&
150
            strstr(URLDecode::getURI(), '#') === false);
151
	}
152
153
}
154