Passed
Push — master ( 0eacc5...f4256c )
by Maxence
05:56
created

MiscService::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Service;
28
29
use OCA\CMSPico\AppInfo\Application;
30
use OCA\CMSPico\Exceptions\MissingKeyInArrayException;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\DataResponse;
33
use OCP\ILogger;
34
35
class MiscService {
36
37
	const ALPHA = 'abcdefghijklmnopqrstuvwxyz';
38
	const ALPHA_NUMERIC = 'abcdefghijklmnopqrstuvwxyz0123456789';
39
	const ALPHA_NUMERIC_SCORES = 'abcdefghijklmnopqrstuvwxyz0123456789_-';
40
41
	/** @var ILogger */
42
	private $logger;
43
44
	public function __construct(ILogger $logger) {
45
		$this->logger = $logger;
46
	}
47
48
	/**
49
	 * @param string $message
50
	 * @param int $level
51
	 */
52
	public function log($message, $level = 2) {
53
		$data = array(
54
			'app'   => Application::APP_NAME,
55
			'level' => $level
56
		);
57
58
		$this->logger->log($level, $message, $data);
59
	}
60
61
62
	/**
63
	 * @param string $path
64
	 */
65
	public static function endSlash(&$path) {
66
		if ($path === '') {
67
			return;
68
		}
69
70
		if (substr($path, -1, 1) !== '/') {
71
			$path .= '/';
72
		}
73
	}
74
75
76
77
78
	/**
79
	 * @param $arr
80
	 * @param $k
81
	 *
82
	 * @param string $default
83
	 *
84
	 * @return array|string
85
	 */
86
	public static function get($arr, $k, $default = '') {
87
		if (!key_exists($k, $arr)) {
88
			return $default;
89
		}
90
91
		return $arr[$k];
92
	}
93
94
95
	public static function mustContains($data, $arr) {
96
		if (!is_array($arr)) {
97
			$arr = [$arr];
98
		}
99
100
		foreach ($arr as $k) {
101
			if (!key_exists($k, $data)) {
102
				throw new MissingKeyInArrayException('missing_key_in_array');
103
			}
104
		}
105
	}
106
107
108
109
110
111
	public static function checkChars($line, $chars) {
112
		for ($i = 0; $i < strlen($line); $i++) {
113
			if (strpos($chars, substr($line, $i, 1)) === false) {
114
				return false;
115
			}
116
		}
117
118
		return true;
119
	}
120
121
	/**
122
	 * @param array $data
123
	 *
124
	 * @return DataResponse
125
	 */
126
	public function fail($data) {
127
		$this->log(json_encode($data));
128
129
		return new DataResponse(
130
			array_merge($data, array('status' => 0)),
131
			Http::STATUS_NON_AUTHORATIVE_INFORMATION
132
		);
133
	}
134
135
136
	/**
137
	 * @param array $data
138
	 *
139
	 * @return DataResponse
140
	 */
141
	public function success($data) {
142
		return new DataResponse(
143
			array_merge($data, array('status' => 1)),
144
			Http::STATUS_CREATED
145
		);
146
	}
147
148
}
149
150