Completed
Push — master ( 5428e7...bccbfd )
by Justin
03:47
created

Sidebar::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
nc 2
nop 3
dl 0
loc 16
rs 10
c 1
b 0
f 1
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: JuKuCMS
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
	public function __construct() {
34
		//
35
	}
36
37
	public function load (int $sidebar_id) {
38
		if (Cache::contains("sidebars", "sidebar_" . $sidebar_id)) {
39
			$this->row = Cache::get("sidebars", "sidebar_" . $sidebar_id);
40
		} else {
41
			//get sidebar from database
42
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array(
43
				'sidebar_id' => $sidebar_id
44
			));
45
46
			if (!$row) {
47
				throw new IllegalStateException("sidebar with sidebar_id '" . $sidebar_id . "' doesnt exists.");
48
			}
49
50
			$this->row = $row;
51
52
			//cache row
53
			Cache::put("sidebars", "sidebar_" . $sidebar_id, $this->row);
54
		}
55
56
		$this->sidebar_id = $sidebar_id;
57
	}
58
59
	/**
60
	 * @return int sidebar id
61
	 */
62
	public function getSidebarId(): int {
63
		return $this->sidebar_id;
64
	}
65
66
	public function getUniqueName () : string {
67
		return $this->row['unique_name'];
68
	}
69
70
	public function getTitle () : string {
71
		return $this->row['title'];
72
	}
73
74
	public function isDeletable () : bool {
75
		return $this->row['deletable'] == 1;
76
	}
77
78
	public function getRow () : array {
79
		return $this->row;
80
	}
81
82
	public static function create (string $title, string $unique_name, bool $deletable = true) : int {
83
		if (empty($unique_name)) {
84
			throw new IllegalArgumentException("unique_name cannot be null.");
85
		}
86
87
		Database::getInstance()->execute("INSERT INTO `{praefix}sidebars` (
88
			`sidebar_id`, `unique_name`, `title`, `deletable`
89
		) VALUES (
90
			NULL, :unique_name, :title, :deletable
91
		) ON DUPLICATE KEY UPDATE `unique_name` = :unique_name, `title` = :title, `deletable` = :deletable; ", array(
92
			'unique_name' => $unique_name,
93
			'title' => $title,
94
			'deletable' => ($deletable ? 1 : 0)
95
		));
96
97
		return Database::getInstance()->lastInsertId();
98
	}
99
100
	public static function removeById (int $id) {
101
		Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array(
102
			'sidebar_id' => $id
103
		));
104
105
		//clear cache
106
		Cache::clear("sidebars");
107
	}
108
109
	public static function removeByUniqueName (string $unique_name) {
110
		if (empty($unique_name)) {
111
			throw new IllegalArgumentException("unique_name cannot be null.");
112
		}
113
114
		Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `unique_name` = :unique_name; ", array(
115
			'unique_name' => $unique_name
116
		));
117
118
		//clear cache
119
		Cache::clear("sidebars");
120
	}
121
122
}
123
124
?>
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...
125