Passed
Push — master ( 2f56fd...c7e98d )
by Anton
05:11 queued 01:55
created

Tables::getDefinitions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace Modules\Install\Utils {
4
5
	use Modules\Entitizer, Modules\Informer, DB, Language, Template;
6
7
	abstract class Tables {
8
9
		# Get definitions list
10
11
		private static function getDefinitions(bool $reverse) {
12
13
			$definitions = [];
14
15
			$definitions[] = Entitizer::definition(TABLE_PAGES);
16
17
			$definitions[] = Entitizer::definition(TABLE_MENU);
18
19
			$definitions[] = Entitizer::definition(TABLE_VARIABLES);
20
21
			$definitions[] = Entitizer::definition(TABLE_WIDGETS);
22
23
			$definitions[] = Entitizer::definition(TABLE_USERS);
24
25
			$definitions[] = Entitizer::definition(TABLE_USERS_SECRETS);
26
27
			$definitions[] = Entitizer::definition(TABLE_USERS_SESSIONS);
28
29
			# ------------------------
30
31
			return ($reverse ? array_reverse($definitions) : $definitions);
32
		}
33
34
		# Fill specific relations table
35
36
		private static function fillRelationsTable(string $table, int $max_id) {
37
38
			$relations = [];
39
40
			for ($id = 1; $id <= $max_id; $id++) {
41
42
				$relations[] = ['ancestor' => $id, 'descendant' => $id, 'depth' => 0];
43
			}
44
45
			# ------------------------
46
47
			return (DB::insert($table, $relations, true, true) && DB::getLast()->status);
48
		}
49
50
		# Fill pages table
51
52
		private static function fillPagesTable() {
53
54
			# Count pages
55
56
			$count = Informer::countEntries(TABLE_PAGES, true);
57
58 View Code Duplication
			if (false === $count) return false; else if ($count > 0) return true;
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...
59
60
			# Process dataset
61
62
			$pages = [['id' => 1, 'visibility' => VISIBILITY_PUBLISHED,
63
64
				'locked' => false, 'slug' => 'index', 'name' => 'index',
65
66
				'title' => Language::get('INSTALL_PAGE_INDEX_TITLE'),
67
68
				'contents' => Template::createBlock(Language::get('INSTALL_PAGE_INDEX_CONTENTS'))->getContents(),
69
70
				'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME]];
71
72
			for ($id = 2; $id <= 4; $id++) $pages[] = ['id' => $id, 'visibility' => VISIBILITY_PUBLISHED,
73
74
				'locked' => false, 'slug' => ('page-' . ($id - 1)), 'name' => ('page-' . ($id - 1)),
75
76
				'title' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . ($id - 1)),
77
78
				'contents' => Template::createBlock(Language::get('INSTALL_PAGE_DEMO_CONTENTS'))->getContents(),
79
80
				'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME];
81
82
			# Process insertion
83
84 View Code Duplication
			if (!(DB::insert(TABLE_PAGES, $pages, true) && DB::getLast()->status)) return false;
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...
85
86
			self::fillRelationsTable(TABLE_PAGES_RELATIONS, 4);
87
88
			# ------------------------
89
90
			return true;
91
		}
92
93
		# Fill menu table
94
95
		private static function fillMenuTable() {
96
97
			# Count menuitems
98
99
			$count = Informer::countEntries(TABLE_MENU, true);
100
101 View Code Duplication
			if (false === $count) return false; else if ($count > 0) return true;
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...
102
103
			# Process dataset
104
105
			$menu = [];
106
107
			for ($id = 1; $id <= 3; $id++) $menu[] = [
108
109
				'id' => $id, 'active' => true, 'position' => ($id - 1), 'slug' => ('page-' . $id),
110
111
				'text' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . $id)];
112
113
			# Process insertion
114
115 View Code Duplication
			if (!(DB::insert(TABLE_MENU, $menu, true) && DB::getLast()->status)) return false;
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...
116
117
			self::fillRelationsTable(TABLE_MENU_RELATIONS, 3);
118
119
			# ------------------------
120
121
			return true;
122
		}
123
124
		# Create tables
125
126
		public static function create() {
127
128
			$definitions = self::getDefinitions(false);
129
130
			foreach ($definitions as $definition) if (!$definition->createTables()) return false;
131
132
			# ------------------------
133
134
			return true;
135
		}
136
137
		# Remove tables
138
139
		public static function remove() {
140
141
			$definitions = self::getDefinitions(true);
142
143
			foreach ($definitions as $definition) if (!$definition->removeTables()) return false;
144
145
			# ------------------------
146
147
			return true;
148
		}
149
150
		# Fill tables
151
152
		public static function fill() {
153
154
			return (self::fillPagesTable() && self::fillMenuTable());
155
		}
156
	}
157
}
158