Completed
Branch 0.2.1 (e70612)
by Anton
09:15
created

Settings::set()   C

Complexity

Conditions 22
Paths 18

Size

Total Lines 75
Code Lines 22

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 75
rs 5.2489
cc 22
eloc 22
nc 18
nop 2

How to fix   Long Method    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 Modules {
4
5
	use Utils\Lister, Explorer, Geo\Timezone, Validate;
6
7
	abstract class Settings {
8
9
		private static $loaded = false;
10
11
		# Params
12
13
		private static $settings = [
14
15
			'admin_language'        => CONFIG_ADMIN_LANGUAGE_DEFAULT,
16
			'admin_template'        => CONFIG_ADMIN_TEMPLATE_DEFAULT,
17
18
			'site_language'         => CONFIG_SITE_LANGUAGE_DEFAULT,
19
			'site_template'         => CONFIG_SITE_TEMPLATE_DEFAULT,
20
21
			'site_title'            => CONFIG_SITE_TITLE_DEFAULT,
22
			'site_slogan'           => CONFIG_SITE_SLOGAN_DEFAULT,
23
24
			'site_status'           => STATUS_ONLINE,
25
26
			'site_description'      => '',
27
			'site_keywords'         => '',
28
29
			'system_url'            => CONFIG_SYSTEM_URL_DEFAULT,
30
			'system_email'          => CONFIG_SYSTEM_EMAIL_DEFAULT,
31
			'system_timezone'       => CONFIG_SYSTEM_TIMEZONE_DEFAULT,
32
33
			'users_registration'    => false
34
		];
35
36
		# Init settings
37
38
		public static function init() {
39
40
			$settings_file = (DIR_SYSTEM_DATA . 'Settings.json');
41
42
			self::$loaded = (false !== ($settings = Explorer::json($settings_file)));
43
44
			if (self::$loaded) foreach ($settings as $name => $value) self::set($name, $value);
45
46
			# ------------------------
47
48
			return true;
49
		}
50
51
		# Save settings
52
53
		public static function save() {
54
55
			$settings_file = (DIR_SYSTEM_DATA . 'Settings.json');
56
57
			if (false === ($settings = json_encode(self::$settings, JSON_PRETTY_PRINT))) return false;
58
59
			if (false === Explorer::save($settings_file, $settings, true)) return false;
60
61
			# ------------------------
62
63
			return true;
64
		}
65
66
		# Check if settings file is loaded
67
68
		public static function loaded() {
69
70
			return self::$loaded;
71
		}
72
73
		# Set value
74
75
		public static function set(string $name, $value) {
76
77
			if (!isset(self::$settings[$name])) return false;
78
79
			# Validate language
80
81
			if (($name === 'admin_language') || ($name === 'site_language')) {
82
83
				if (false === ($value = Extend\Languages::validate($value))) return false;
84
			}
85
86
			# Validate template
87
88
			else if (($name === 'admin_template') || ($name === 'site_template')) {
89
90
				if (false === ($value = Extend\Templates::validate($value))) return false;
91
			}
92
93
			# Validate site title
94
95
			else if ($name === 'site_title') {
96
97
				if ('' === ($value = (function(string $value) { return $value; })($value))) return false;
98
			}
99
100
			# Validate site status
101
102
			else if ($name === 'site_status') {
103
104
				if (false === ($value = Lister\Status::validate($value))) return false;
105
			}
106
107
			# Validate site slogan/description/keywords
108
109
			else if (($name === 'site_slogan') || ($name === 'site_description') || ($name === 'site_keywords')) {
110
111
				$value = (function(string $value) { return $value; })($value);
112
			}
113
114
			# Validate system url
115
116
			else if ($name === 'system_url') {
117
118
				if (false === ($value = Validate::url($value))) return false;
119
			}
120
121
			# Validate system email
122
123
			else if ($name === 'system_email') {
124
125
				if (false === ($value = Validate::email($value))) return false;
126
			}
127
128
			# Validate system timezone
129
130
			else if ($name === 'system_timezone') {
131
132
				if (false === ($value = Timezone::validate($value))) return false;
133
			}
134
135
			# Validate users registration
136
137
			else if ($name === 'users_registration') {
138
139
				$value = (function(bool $value) { return $value; })($value);
140
			}
141
142
			# Set value
143
144
			self::$settings[$name] = $value;
145
146
			# ------------------------
147
148
			return true;
149
		}
150
151
		# Get value
152
153
		public static function get(string $name = null) {
154
155
			if (null === $name) return self::$settings;
156
157
			return (isset(self::$settings[$name]) ? self::$settings[$name] : null);
158
		}
159
	}
160
}
161