Completed
Push — master ( 2db1fb...5408f9 )
by Nazar
04:38
created

Module_Properties::uninstalled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Config;
9
use
10
	cs\Config;
11
/**
12
 * Class for getting of db and storage configuration of module
13
 */
14
class Module_Properties {
15
	const ENABLED     = 1;
16
	const DISABLED    = 0;
17
	const UNINSTALLED = -1;
18
	/**
19
	 * @var array
20
	 */
21
	protected $module_data = [];
22
	/**
23
	 * @var string
24
	 */
25
	protected $module;
26
	/**
27
	 * Creating of object and saving module data inside
28
	 *
29
	 * @param array  $module_data
30
	 * @param string $module
31
	 */
32 58
	public function __construct ($module_data, $module) {
33 58
		$this->module_data = $module_data;
34 58
		$this->module      = $module;
35 58
	}
36
	/**
37
	 * Whether module is enabled
38
	 *
39
	 * @return bool
40
	 */
41 4
	public function enabled () {
42 4
		return $this->module_data['active'] == self::ENABLED;
43
	}
44
	/**
45
	 * Whether module is disabled
46
	 *
47
	 * @return bool
48
	 */
49
	public function disabled () {
50
		return $this->module_data['active'] == self::DISABLED;
51
	}
52
	/**
53
	 * Whether module is installed
54
	 *
55
	 * @return bool
56
	 */
57
	public function installed () {
58
		return $this->module_data['active'] != self::UNINSTALLED;
59
	}
60
	/**
61
	 * Whether module is uninstalled
62
	 *
63
	 * @return bool
64
	 */
65 2
	public function uninstalled () {
66 2
		return $this->module_data['active'] == self::UNINSTALLED;
67
	}
68
	/**
69
	 * Get db id by name
70
	 *
71
	 * @param string $db_name
72
	 *
73
	 * @return int
74
	 */
75 38
	public function db ($db_name) {
76 38
		return $this->module_data['db'][$db_name];
77
	}
78
	/**
79
	 * Get storage id by name
80
	 *
81
	 * @param string $storage_name
82
	 *
83
	 * @return int
84
	 */
85
	public function storage ($storage_name) {
86
		return $this->module_data['storage'][$storage_name];
87
	}
88
	/**
89
	 * Get data item of module configuration
90
	 *
91
	 * @param string $item
92
	 *
93
	 * @return false|mixed
94
	 */
95
	public function __get ($item) {
96
		return $this->get($item);
97
	}
98
	/**
99
	 * Set data item of module configuration (only for admin)
100
	 *
101
	 * @param string $item
102
	 * @param mixed  $value
103
	 */
104
	public function __set ($item, $value) {
105
		$this->set_internal($item, $value);
106
	}
107
	/**
108
	 * Get data item (or array of items) of module configuration
109
	 *
110
	 * @param string|string[] $item
111
	 *
112
	 * @return false|mixed|mixed[]
113
	 */
114
	public function get ($item) {
115
		if (is_array($item)) {
116
			$result = [];
117
			foreach ($item as $i) {
118
				$result[$i] = $this->get($i);
119
			}
120
			return $result;
121
		} elseif (isset($this->module_data['data'], $this->module_data['data'][$item])) {
122
			return $this->module_data['data'][$item];
123
		} else {
124
			return false;
125
		}
126
	}
127
	/**
128
	 * Set data item (or array of items) of module configuration (only for admin)
129
	 *
130
	 * @param array|string $item
131
	 * @param mixed|null   $value
132
	 *
133
	 * @return bool
134
	 *
135
	 * @throws \cs\ExitException
136
	 */
137
	public function set ($item, $value = null) {
138
		if (is_array($item)) {
139
			/** @noinspection SuspiciousLoopInspection */
140
			foreach ($item as $i => $value) {
141
				$this->set_internal($i, $value, false);
142
			}
143
			return Config::instance()->save();
144
		} else {
145
			return $this->set_internal($item, $value);
146
		}
147
	}
148
	protected function set_internal ($item, $value, $save = true) {
149
		$Config      = Config::instance();
150
		$module_data = &$Config->components['modules'][$this->module];
151
		if (!isset($module_data['data'])) {
152
			$module_data['data'] = [];
153
		}
154
		$module_data['data'][$item] = $value;
155
		$this->module_data          = $module_data;
156
		if ($save) {
157
			return $Config->save();
158
		}
159
		return true;
160
	}
161
}
162