Definition   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 202
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 4

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatements() 0 13 3
A createMainTable() 0 12 2
A createRelationsTable() 0 20 2
A removeMainTable() 0 6 2
A removeRelationsTable() 0 6 2
A __construct() 0 12 1
A getParam() 0 4 1
A getParams() 0 4 1
A getParamsSecure() 0 4 1
A getIndex() 0 4 1
A getIndexes() 0 4 1
A getForeign() 0 4 1
A getForeigns() 0 4 1
A createTables() 0 4 3
A removeTables() 0 4 3
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Entitizer
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Entitizer\Utils {
11
12
	use DB;
13
14
	abstract class Definition {
15
16
		protected $params = null, $indexes = null, $foreigns = null;
17
18
		/**
19
		 * Get the list of statements
20
		 */
21
22
		private function getStatements() : array {
23
24
			$statements = [];
25
26
			foreach ([$this->params, $this->indexes, $this->foreigns] as $group) {
27
28
				foreach ($group->getList() as $item) $statements[] = $item->getStatement();
29
			}
30
31
			# ------------------------
32
33
			return $statements;
34
		}
35
36
		/**
37
		 * Create the main table
38
		 *
39
		 * @return bool : true on success or false on failure
40
		 */
41
42
		private function createMainTable() : bool {
43
44
			$query = ("CREATE TABLE IF NOT EXISTS `" . static::$table . "` (") .
45
46
			         implode(", ", $this->getStatements()) .
47
48
			         (") ENGINE=InnoDB DEFAULT CHARSET=utf8");
49
50
			# ------------------------
51
52
			return (DB::send($query) && DB::getLast()->status);
53
		}
54
55
		/**
56
		 * Create the relations table
57
		 *
58
		 * @return bool : true on success or false on failure
59
		 */
60
61
		private function createRelationsTable() : bool {
62
63
			$query = ("CREATE TABLE IF NOT EXISTS `" . static::$table_relations . "` (") .
64
65
					 ("`ancestor` int(10) UNSIGNED NOT NULL, `descendant` int(10) UNSIGNED NOT NULL, ") .
66
67
					 ("`depth` int(10) UNSIGNED NOT NULL, ") .
68
69
					 ("PRIMARY KEY (`ancestor`, `descendant`), KEY (`ancestor`), KEY (`descendant`), KEY (`depth`), ") .
70
71
					 ("FOREIGN KEY (`ancestor`) REFERENCES `" . static::$table . "` (`id`) ON DELETE CASCADE, ") .
72
73
					 ("FOREIGN KEY (`descendant`) REFERENCES `" . static::$table . "` (`id`) ON DELETE CASCADE ") .
74
75
					 (") ENGINE=InnoDB DEFAULT CHARSET=utf8");
76
77
			# ------------------------
78
79
		 	return (DB::send($query) && DB::getLast()->status);
80
		}
81
82
		/**
83
		 * Drop the main table
84
		 *
85
		 * @return bool : true on success or false on failure
86
		 */
87
88
		private function removeMainTable() : bool {
89
90
			$query = ("DROP TABLE IF EXISTS `" . static::$table  . "`");
91
92
			return (DB::send($query) && DB::getLast()->status);
93
		}
94
95
		/**
96
		 * Drop the relations table
97
		 *
98
		 * @return bool : true on success or false on failure
99
		 */
100
101
		private function removeRelationsTable() : bool {
102
103
			$query = ("DROP TABLE IF EXISTS `" . static::$table_relations  . "`");
104
105
			return (DB::send($query) && DB::getLast()->status);
106
		}
107
108
		/**
109
		 * Constructor
110
		 */
111
112
		public function __construct() {
113
114
			$this->params = new Definition\Group\Params($this, static::$auto_increment);
115
116
			$this->indexes = new Definition\Group\Indexes($this);
117
118
			$this->foreigns = new Definition\Group\Foreigns($this);
119
120
			# Define specific params
121
122
			$this->define();
123
		}
124
125
		/**
126
		 * Get a param
127
		 *
128
		 * @return Entitizer\Utils\Definition\Item\Param|false : the param object or false if the param does not exist
129
		 */
130
131
		public function getParam(string $name) {
132
133
			return $this->params->get($name);
134
		}
135
136
		/**
137
		 * Get the params list
138
		 */
139
140
		public function getParams() : array {
141
142
			return $this->params->getList();
143
		}
144
145
		/**
146
		 * Get the secure params list (includes every param except of long textuals)
147
		 */
148
149
		public function getParamsSecure() : array {
150
151
			return $this->params->getSecure();
152
		}
153
154
		/**
155
		 * Get an index
156
		 *
157
		 * @return Entitizer\Utils\Definition\Item\Index|false : the index object or false if the index does not exist
158
		 */
159
160
		public function getIndex(string $name) {
161
162
			return $this->indexes->get($name);
163
		}
164
165
		/**
166
		 * Get the indexes list
167
		 */
168
169
		public function getIndexes() : array {
170
171
			return $this->indexes->getList();
172
		}
173
174
		/**
175
		 * Get a foreign
176
		 *
177
		 * @return Entitizer\Utils\Definition\Item\Foreign|false : the foreign object or false if the foreign does not exist
178
		 */
179
180
		public function getForeign(string $name) {
181
182
			return $this->foreigns->get($name);
183
		}
184
185
		/**
186
		 * Get the foreigns list
187
		 */
188
189
		public function getForeigns() : array {
190
191
			return $this->foreigns->getList();
192
		}
193
194
		/**
195
		 * Create entity table(s)
196
		 *
197
		 * @return bool : true on success or false on failure
198
		 */
199
200
		public function createTables() : bool {
201
202
			return ($this->createMainTable() && (!static::$nesting || $this->createRelationsTable()));
203
		}
204
205
		/**
206
		 * Remove entity table(s)
207
		 *
208
		 * @return bool : true on success or false on failure
209
		 */
210
211
		public function removeTables() : bool {
212
213
			return ((!static::$nesting || $this->removeRelationsTable()) && $this->removeMainTable());
214
		}
215
	}
216
}
217