Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

Section::section()   D

Complexity

Conditions 17
Paths 26

Size

Total Lines 45
Code Lines 15

Duplication

Lines 4
Ratio 8.89 %
Metric Value
dl 4
loc 45
rs 4.9807
cc 17
eloc 15
nc 26
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
<?php
2
3
namespace Frames\Admin {
4
5
	use Frames, Frames\Status, Modules\Auth, Modules\Extend, Utils\Messages, Utils\View;
6
	use Ajax, DB, Debug, Request, Template;
7
8
	abstract class Section extends Frames\Section {
9
10
		# Define active section
11
12
		const SECTION = SECTION_ADMIN;
13
14
		# Define phrases list
15
16
		const PHRASES = ['Admin', 'Ajax', 'Common', 'Install', 'Lister', 'Mail', 'Menuitem', 'Page', 'User', 'Variable', 'Widget'];
17
18
		# Section settings
19
20
		protected $title = '';
21
22
		# Display form
23
24
		private function displayForm(Template\Asset\Block $contents, string $status) {
25
26
			$form = View::get('Main\Form');
27
28
			# Set language
29
30
			$form->language = Extend\Languages::data('iso');
31
32
			# Set title
33
34
			$form->title = ((('' !== $this->title) ? ($this->title . ' | ') : '') . CADMIUM_NAME);
35
36
			# Create layout
37
38
			$form->layout = ($layout = View::get('Layouts\Form'));
39
40
			# Set title
41
42
			$layout->title = $this->title;
43
44
			# Set messages
45
46
			$layout->messages = Messages::block();
47
48
			# Set contents
49
50
			$layout->contents = $contents;
51
52
			# Set report
53
54
			$layout->block('report')->script_time = Debug::time();
55
56
			$layout->block('report')->db_time = DB::time();
57
58
			# ------------------------
59
60
			Template::output($form, $status);
61
		}
62
63
		# Display page
64
65
		private function displayPage(Template\Asset\Block $contents) {
66
67
			$page = View::get('Main\Page');
68
69
			# Set language
70
71
			$page->language = Extend\Languages::data('iso');
72
73
			# Set title
74
75
			$page->title = ((('' !== $this->title) ? ($this->title . ' | ') : '') . CADMIUM_NAME);
76
77
			# Create layout
78
79
			$page->layout = ($layout = View::get('Layouts\Page'));
80
81
			# Set menu
82
83
			$layout->menu = View::get('Blocks\Utils\Menu');
84
85
			# Set user
86
87
			$layout->block('user')->gravatar = md5(strtolower(Auth::user()->email));
88
89
			$layout->block('user')->name = Auth::user()->name;
90
91
			$layout->block('user')->id = Auth::user()->id;
92
93
			# Set title
94
95
			$layout->title = $this->title;
96
97
			# Set messages
98
99
			$layout->messages = Messages::block();
100
101
			# Set contents
102
103
			$layout->contents = $contents;
104
105
			# Set copyright
106
107
			$layout->cadmium_home       = CADMIUM_HOME;
108
			$layout->cadmium_copy       = CADMIUM_COPY;
109
			$layout->cadmium_name       = CADMIUM_NAME;
110
			$layout->cadmium_version    = CADMIUM_VERSION;
111
112
			# Set language selector
113
114
			$layout->block('language')->country = Extend\Languages::data('country');
115
116
			$layout->block('language')->title = Extend\Languages::data('title');
117
118
			$layout->block('language')->items = Extend\Languages::items();
119
120
			# Set template selector
121
122
			$layout->block('template')->title = Extend\Templates::data('title');
123
124
			$layout->block('template')->items = Extend\Templates::items();
125
126
			# Set report
127
128
			$layout->block('report')->script_time = Debug::time();
129
130
			$layout->block('report')->db_time = DB::time();
131
132
			# ------------------------
133
134
			Template::output($page, STATUS_CODE_200);
135
		}
136
137
		# Admin main method
138
139
		protected function section() {
140
141
			# Check for restricted access
142
143
			if (('' === CONFIG_ADMIN_IP) || in_array(REQUEST_CLIENT_IP, preg_split('/ +/', CONFIG_ADMIN_IP))) {
144
145
				# Handle install component request
146
147
				if ($this instanceof Component\Install) {
148
149
					if (Template::isBlock($result = $this->handle())) return $this->displayForm($result, STATUS_CODE_200);
150
				}
151
152
				# Handle auth component request
153
154
				else if ($this instanceof Component\Auth) {
155
156
					if (Auth::check()) Request::redirect(INSTALL_PATH . '/admin');
157
158
					if ($this instanceof Component\Auth\Initial) { if (!Auth::initial()) Request::redirect(INSTALL_PATH . '/admin/login'); }
159
160
					else if (Auth::initial()) Request::redirect(INSTALL_PATH . '/admin/register');
161
162
					if (Template::isBlock($result = $this->handle())) return $this->displayForm($result, STATUS_CODE_401);
163
				}
164
165
				# Handle panel component request
166
167
				else if ($this instanceof Component\Panel) {
168
169 View Code Duplication
					if (!Auth::check() || ((false !== Request::get('logout')) && Auth::logout())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
171
						Request::redirect(INSTALL_PATH . '/admin/login');
172
					}
173
174
					if (Template::isBlock($result = $this->handle())) return $this->displayPage($result);
175
176
					if (Ajax::isResponse($result)) return Ajax::output($result);
177
				}
178
			}
179
180
			# ------------------------
181
182
			return Status::error404();
183
		}
184
	}
185
}
186