Completed
Push — master ( b78077...b534a9 )
by Justin
03:18
created

Sidebar::initialize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.9666
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
	protected static $is_initialized = false;
34
	protected static $all_sidebars = array();
35
36
	public function __construct() {
37
		//load in-memory cache, if neccessary
38
		if (!self::isInitialized()) {
39
			self::initialize();
40
		}
41
	}
42
43
	public function load (int $sidebar_id) {
44
		if (isset(self::$all_sidebars[$sidebar_id])) {
45
			$this->row = self::$all_sidebars[$sidebar_id];
46
		} else {
47
			throw new IllegalStateException("sidebar with id '" . $sidebar_id . "' doesnt exists.");
48
		}
49
50
		$this->sidebar_id = $sidebar_id;
51
	}
52
53
	/**
54
	 * @return int sidebar id
55
	 */
56
	public function getSidebarId(): int {
57
		return $this->sidebar_id;
58
	}
59
60
	public function getUniqueName () : string {
61
		return $this->row['unique_name'];
62
	}
63
64
	public function getTitle () : string {
65
		return $this->row['title'];
66
	}
67
68
	public function isDeletable () : bool {
69
		return $this->row['deletable'] == 1;
70
	}
71
72
	public function getRow () : array {
73
		return $this->row;
74
	}
75
76
	/**
77
	 * @return bool
78
	 */
79
	protected static function isInitialized(): bool {
80
		return self::$is_initialized;
81
	}
82
83
	protected static function initialize () {
84
		if (Cache::contains("sidebars", "all_sidebars")) {
85
			self::$all_sidebars = Cache::get("sidebars", "all_sidebars");
86
		} else {
87
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; ");
88
89
			//clear in-memory cache
90
			self::$all_sidebars = array();
91
92
			foreach ($rows as $row) {
93
				self::$all_sidebars[$row['sidebar_id']] = $row;
94
			}
95
96
			//cache result
97
			Cache::put("sidebars", "all_sidebars");
0 ignored issues
show
Bug introduced by
The call to Cache::put() has too few arguments starting with value. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
			Cache::/** @scrutinizer ignore-call */ 
98
          put("sidebars", "all_sidebars");

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
98
		}
99
100
		self::$is_initialized = true;
101
	}
102
103
	public static function create (string $title, string $unique_name, bool $deletable = true) : int {
104
		if (empty($unique_name)) {
105
			throw new IllegalArgumentException("unique_name cannot be null.");
106
		}
107
108
		Database::getInstance()->execute("INSERT INTO `{praefix}sidebars` (
109
			`sidebar_id`, `unique_name`, `title`, `deletable`
110
		) VALUES (
111
			NULL, :unique_name, :title, :deletable
112
		) ON DUPLICATE KEY UPDATE `unique_name` = :unique_name, `title` = :title, `deletable` = :deletable; ", array(
113
			'unique_name' => $unique_name,
114
			'title' => $title,
115
			'deletable' => ($deletable ? 1 : 0)
116
		));
117
118
		//clear cache
119
		Cache::clear("sidebars");
120
121
		return Database::getInstance()->lastInsertId();
122
	}
123
124
	public static function removeById (int $id) {
125
		Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array(
126
			'sidebar_id' => $id
127
		));
128
129
		//clear cache
130
		Cache::clear("sidebars");
131
	}
132
133
	public static function removeByUniqueName (string $unique_name) {
134
		if (empty($unique_name)) {
135
			throw new IllegalArgumentException("unique_name cannot be null.");
136
		}
137
138
		Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `unique_name` = :unique_name; ", array(
139
			'unique_name' => $unique_name
140
		));
141
142
		//clear cache
143
		Cache::clear("sidebars");
144
	}
145
146
}
147
148
?>
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...
149