Passed
Pull Request — master (#15)
by Anton
03:20
created

Section::_section()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 7
nc 6
nop 0
1
<?php
2
3
/**
4
 * @package Cadmium\System\Frames\Site
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Frames\Site {
11
12
	use Frames, Frames\Status, Modules\Auth, Modules\Settings, Ajax, Request, Template;
13
14
	abstract class Section extends Frames\Section {
15
16
		# Define active section
17
18
		const SECTION = SECTION_SITE;
19
20
		# Define phrases list
21
22
		const PHRASES = ['Common', 'Mail', 'Range', 'Site', 'User'];
23
24
		# Page settings
25
26
		protected $_title = '', $_layout = 'Common';
27
28
		/**
29
		 * Handle an auth area request
30
		 */
31
32
		private function handleAuthArea() {
33
34
			# Check auth
35
36
			if (Auth::check()) Request::redirect(INSTALL_PATH . '/profile');
37
38
			# ------------------------
39
40
			return $this->handleCommonArea();
41
		}
42
43
		/**
44
		 * Handle an authorized area request
45
		 */
46
47
		private function handleAuthorizedArea() {
48
49
			# Check auth
50
51
			if (!Auth::check() || ((false !== Request::get('logout')) && Auth::logout())) {
52
53
				Request::redirect(INSTALL_PATH . '/profile/login');
54
			}
55
56
			# ------------------------
57
58
			return $this->handleCommonArea();
59
		}
60
61
		/**
62
		 * Handle a common area request
63
		 */
64
65
		private function handleCommonArea() {
66
67
			# Handle request
68
69
			$result = $this->handle(Request::isAjax());
0 ignored issues
show
Unused Code introduced by
The call to Section::handle() has too many arguments starting with \Request::isAjax().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
71
			if (Template::isBlock($result)) return (new View($this->_title, $this->_layout))->display($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->handle(\Request::isAjax()) on line 69 can also be of type null; however, Frames\Site\View::display() does only seem to accept object<Template\Block>, 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...
72
73
			if (Ajax::isResponse($result)) return Ajax::output($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->handle(\Request::isAjax()) on line 69 can also be of type null or object<Template\Block>; however, Ajax::output() does only seem to accept object<Ajax\Response>, 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...
74
75
			# ------------------------
76
77
			return Status::displayError404();
78
		}
79
80
		/**
81
		 * The branch method for the site section
82
		 */
83
84
		protected function _section() {
85
86
			# Check site status
87
88
			if (Settings::get('site_status') === STATUS_MAINTENANCE) return Status::displayMaintenance();
89
90
			if (Settings::get('site_status') === STATUS_UPDATE) return Status::displayUpdate();
91
92
			# Handle request
93
94
			if ($this instanceof Area\Auth) return $this->handleAuthArea();
95
96
			if ($this instanceof Area\Authorized) return $this->handleAuthorizedArea();
97
98
			if ($this instanceof Area\Common) return $this->handleCommonArea();
99
100
			# ------------------------
101
102
			return Status::displayError404();
103
		}
104
	}
105
}
106