Test Failed
Push — master ( 249189...0dbce5 )
by Justin
05:02
created

Events::init()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 11
nc 2
nop 0
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: 04.03.2018
25
 * Time: 19:05
26
 */
27
28
class Events {
29
30
	protected static $events = array();
31
32
	protected static $isInitialized = false;
33
34
	public static function init () {
35
		if (Cache::getCache()->contains("events", "events")) {
36
			self::$events = Cache::getCache()->get("events", "events");
37
		} else {
38
			//load events from database
39
			$rows = Database::getInstance()->listRows("SELECT * FROM `{PRAEFIX}events` WHERE `activated` = '1'; ");
40
41
			//iterate through rows
42
			foreach ($rows as $row) {
43
				//get name of event
44
				$name = $row['name'];
45
46
				//check, if name exists in array
47
				if (!isset(self::$events[$name])) {
48
					self::$events[$name] = array();
49
				}
50
51
				//add row to array
52
				self::$events[$name][] = $row;
53
			}
54
55
			//put events into cache
56
			Cache::getCache()->put("events", "events", self::$events);
57
		}
58
59
		//set initialized flag to true
60
		self::$isInitialized = true;
61
	}
62
63
	public static function throwEvent ($name, $params = array()) {
64
		if (!is_array($params)) {
65
			throw new IllegalArgumentException("second parameter params has to be an array.");
66
		}
67
68
		//check, if events was initialized first
69
		if (!self::$isInitialized) {
70
			//initialize events
71
			self::init();
72
		}
73
74
		if (isset(self::$events[$name])) {
75
			foreach (self::$events as $event) {
76
				self::executeEvent($event, $params);
77
			}
78
		}
79
	}
80
81
	protected static function executeEvent ($row, $params) {
82
		$type = strtolower($row['type']);
83
		$file = $row['file'];
84
		$class_name = $row['class_name'];
85
		$class_method = $row['class_method'];
86
87
		switch ($type) {
88
			case "file":
89
				//check, if file exists
90
				if (file_exists(ROOT_PATH . $file)) {
91
					require(ROOT_PATH . $file);
92
				} else {
93
					throw new IllegalStateException("required file for event not found: " . $file);
94
				}
95
96
				break;
97
			case "function":
98
				call_user_func($class_method, $params);
99
				break;
100
			case "class_static_method":
101
				call_user_func(array($class_name, $class_method), $params);
102
				break;
103
			default:
104
				throw new IllegalStateException("unknown event type '" . $type . "' for event '" . $row['name'] . "'!");
105
				break;
106
		}
107
	}
108
109
	public static function addEventClass (string $event, string $class_name, string $method, string $plugin_name) {
110
		Database::getInstance()->execute("INSERT INTO `{praefix}events` (
111
			`id`, `name`, `type`, `file`, `class_name`, `class_method`, `created_from`, `activated`
112
		) VALUES (
113
			NULL, :event, 'CLASS_STATIC_METHOD', '', :class_name, :method, :created_from, '1'
114
		) ON DUPLICATE KEY UPDATE `activated` = '1';", array(
115
			'event' => $event,
116
			'class_name' => $class_name,
117
			'method' => $method,
118
			'created_from' => "plugin_" . $plugin_name
119
		));
120
	}
121
122
	public static function removePluginEvents (string $plugin_name) {
123
		Database::getInstance()->execute("DELETE FROM `{praefix}events` WHERE `created_from` = :created_from; ", array(
124
			'created_from' => "plugin_" . $plugin_name
125
		));
126
	}
127
128
}
129
130
?>
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...
131