Completed
Push — master ( b8e700...9bc375 )
by Justin
03:48
created

Sidebar   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 2
Metric Value
eloc 87
c 7
b 0
f 2
dl 0
loc 209
rs 10
wmc 30

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 2 1
A removeByUniqueName() 0 11 2
A listWidgets() 0 2 1
A removeById() 0 7 1
A load() 0 8 2
A __construct() 0 4 2
A initialize() 0 18 3
A getUniqueName() 0 2 1
A listWidgetTplArray() 0 17 2
A getRow() 0 2 1
A getSidebarId() 0 2 1
A create() 0 19 3
A isDeletable() 0 2 1
A isInitialized() 0 2 1
A loadWidgets() 0 34 4
A cast() 0 2 1
A listSidebars() 0 21 3
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 24.08.2018
25
 * Time: 13:44
26
 */
27
28
class Sidebar {
29
30
	protected $sidebar_id = -1;
31
	protected $row = array();
32
33
	protected $widget_rows = array();
34
	protected $widgets = array();
35
36
	protected static $is_initialized = false;
37
	protected static $all_sidebars = array();
38
39
	public function __construct() {
40
		//load in-memory cache, if neccessary
41
		if (!self::isInitialized()) {
42
			self::initialize();
43
		}
44
	}
45
46
	public function load (int $sidebar_id) {
47
		if (isset(self::$all_sidebars[$sidebar_id])) {
48
			$this->row = self::$all_sidebars[$sidebar_id];
49
		} else {
50
			throw new IllegalStateException("sidebar with id '" . $sidebar_id . "' doesnt exists.");
51
		}
52
53
		$this->sidebar_id = $sidebar_id;
54
	}
55
56
	public function loadWidgets () {
57
		$this->widgets = array();
58
59
		//load widgets
60
		if (Cache::contains("sidebars", "sidebar_widgets_" . $this->sidebar_id)) {
61
			$this->widget_rows = Cache::get("sidebars", "sidebar_widgets_" . $this->sidebar_id);
62
		} else {
63
			//list rows from database
64
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebar_widgets` WHERE `sidebar_id` = :sidebar_id ORDER BY `order`; ", array(
65
				'sidebar_id' => $this->sidebar_id
66
			));
67
68
			//cache results
69
			Cache::put("sidebars", "sidebar_widgets_" . $this->sidebar_id, $rows);
70
71
			$this->widget_rows = $rows;
72
		}
73
74
		foreach ($this->widget_rows as $widget_row) {
75
			$class_name = $widget_row['class_name'];
76
			$widget_instance = new $class_name();
77
78
			if (!($widget_instance instanceof Widget)) {
79
				throw new IllegalStateException("instance has to be an instance of class Widget, this means widget types has to extends class Widget.");
80
			}
81
82
			//cast widget
83
			$widget = Widget::castWidget($widget_instance);
84
85
			//load widget and set row, so widget doesn't needs to load data seperate from database
86
			$widget->load($widget_row);
87
88
			//add widget to list
89
			$this->widgets[] = $widget;
90
		}
91
	}
92
93
	/**
94
	 * list widget instances
95
	 */
96
	public function listWidgets () : array {
97
		return $this->widgets;
98
	}
99
100
	public function listWidgetTplArray () : array {
101
		$array = array();
102
103
		foreach ($this->listWidgets() as $widget) {
104
			$widget = Widget::castWidget($widget);
105
106
			$array[] = array(
107
				'id' => $widget->getId(),
108
				'title' => $widget->getTitle(),
109
				'code' => $widget->getCode(),
110
				'css_id' => $widget->getCSSId(),
111
				'css_class' => $widget->getCSSClass(),
112
				'use_template' => $widget->useTemplate()
113
			);
114
		}
115
116
		return $array;
117
	}
118
119
	/**
120
	 * @return int sidebar id
121
	 */
122
	public function getSidebarId(): int {
123
		return $this->sidebar_id;
124
	}
125
126
	public function getUniqueName () : string {
127
		return $this->row['unique_name'];
128
	}
129
130
	public function getTitle () : string {
131
		return $this->row['title'];
132
	}
133
134
	public function isDeletable () : bool {
135
		return $this->row['deletable'] == 1;
136
	}
137
138
	public function getRow () : array {
139
		return $this->row;
140
	}
141
142
	/**
143
	 * @return bool
144
	 */
145
	protected static function isInitialized(): bool {
146
		return self::$is_initialized;
147
	}
148
149
	protected static function initialize () {
150
		if (Cache::contains("sidebars", "all_sidebars")) {
151
			self::$all_sidebars = Cache::get("sidebars", "all_sidebars");
152
		} else {
153
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; ");
154
155
			//clear in-memory cache
156
			self::$all_sidebars = array();
157
158
			foreach ($rows as $row) {
159
				self::$all_sidebars[$row['sidebar_id']] = $row;
160
			}
161
162
			//cache result
163
			Cache::put("sidebars", "all_sidebars", self::$all_sidebars);
164
		}
165
166
		self::$is_initialized = true;
167
	}
168
169
	public static function create (string $title, string $unique_name, bool $deletable = true) : int {
170
		if (empty($unique_name)) {
171
			throw new IllegalArgumentException("unique_name cannot be null.");
172
		}
173
174
		Database::getInstance()->execute("INSERT INTO `{praefix}sidebars` (
175
			`sidebar_id`, `unique_name`, `title`, `deletable`
176
		) VALUES (
177
			NULL, :unique_name, :title, :deletable
178
		) ON DUPLICATE KEY UPDATE `unique_name` = :unique_name, `title` = :title, `deletable` = :deletable; ", array(
179
			'unique_name' => $unique_name,
180
			'title' => $title,
181
			'deletable' => ($deletable ? 1 : 0)
182
		));
183
184
		//clear cache
185
		Cache::clear("sidebars");
186
187
		return Database::getInstance()->lastInsertId();
188
	}
189
190
	public static function removeById (int $id) {
191
		Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array(
192
			'sidebar_id' => $id
193
		));
194
195
		//clear cache
196
		Cache::clear("sidebars");
197
	}
198
199
	public static function removeByUniqueName (string $unique_name) {
200
		if (empty($unique_name)) {
201
			throw new IllegalArgumentException("unique_name cannot be null.");
202
		}
203
204
		Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `unique_name` = :unique_name; ", array(
205
			'unique_name' => $unique_name
206
		));
207
208
		//clear cache
209
		Cache::clear("sidebars");
210
	}
211
212
	public static function listSidebars () : array {
213
		$rows = array();
214
215
		if (Cache::contains("sidebars", "list")) {
216
			$rows = Cache::get("sidebars", "list");
217
		} else {
218
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; ");
219
220
			Cache::put("sidebars", "list", $rows);
221
		}
222
223
		$list = array();
224
225
		foreach ($rows as $row) {
226
			$obj = new Sidebar();
227
			$obj->load($row);
228
229
			$list[] = $obj;
230
		}
231
232
		return $list;
233
	}
234
235
	public static function cast (Sidebar $sidebar) : Sidebar {
236
		return $sidebar;
237
	}
238
239
}
240
241
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
242