Passed
Push — master ( e71961...c8cbb8 )
by Justin
04:01
created

ApiMethod::addMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 8
nc 1
nop 5
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
class ApiMethod {
20
21
	protected $apimethods = array();
22
	protected $method = array();
23
24
	public function __construct () {
25
		//
26
	}
27
28
	public function loadApiMethods () {
29
30
		if (Cache::contains("apimethods", "apimethods")) {
31
			$this->apimethods = Cache::get("apimethods", "apimethods");
32
		} else {
33
			$rows = (Array) DataBase::getInstance()->listRows("SELECT * FROM `{praefix}api_methods` WHERE `activated` = '1'; ");
34
35
			foreach ($rows as $row) {
36
				$row = (Array) $row;
37
				$this->apimethods[$row['api_method']] = $row;
38
			}
39
40
			Cache::put("apimethods", "apimethods", $this->apimethods);
41
		}
42
43
	}
44
45
	public function loadMethod ($method) {
46
		if (isset($this->apimethods[$method])) {
47
			$this->method = $this->apimethods[$method];
48
		}
49
	}
50
51
	public function executeApiMethod () {
52
53
		if (!$this->method) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->method of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
			exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
55
		}
56
57
		if ($this->method['response_type'] != "specific") {
58
			header("Content-Type: " . $this->method['response_type']);
59
		}
60
61
		$classname = $this->method['classname'];
62
		$method = $this->method['method'];
63
64
		$args = array();
65
		Events::throwEvent("apimethods", array('method' => $this->method, 'args' => &$args));
66
67
		$result = call_user_func(array($classname, $method), $args);
68
69
		echo $result;
70
	}
71
72
	public static function addMethod (string $api_method, string $class_name, string $method, string $owner = "system", string $response_type = "") {
73
		//add method to database
74
		Database::getInstance()->execute("INSERT INTO `{praefix}api_methods` (
75
			`api_method`, `classname`, `method`, `response_type`, `owner`, `activated`
76
		) VALUES (
77
			:api_method, :class_name, :method, :response_type, :owner, '1'
78
		) ON DUPLICATE KEY UPDATE `classname` = :class_name, `method` = :method, `response_type` = :response_type, `owner` = :owner, `activated` = '1'; ", array(
79
			'api_method' => $api_method,
80
			'class_name' => $class_name,
81
			'method' => $method,
82
			'response_type' => $response_type,
83
			'owner' => $owner
84
		));
85
86
		//clear cache
87
		Cache::clear("apimethods");
88
	}
89
90
	public static function deleteMethod (string $api_method) {
91
		//delete from database
92
		Database::getInstance()->execute("DELETE FROM `{praefix}api_methods` WHERE `api_method` = :api_method; ", array(
93
			'api_method' => $api_method
94
		));
95
96
		//clear cache
97
		Cache::clear("apimethods");
98
	}
99
100
	public static function deleteMethodsByOwner (string $owner) {
101
		//delete from database
102
		Database::getInstance()->execute("DELETE FROM `{praefix}api_methods` WHERE `owner` = :owner; ", array(
103
			'owner' => $owner
104
		));
105
106
		//clear cache
107
		Cache::clear("apimethods");
108
	}
109
110
}
111
112
?>
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...
113