Passed
Push — master ( 31e7e0...99581b )
by Maxence
02:24
created

MiscService::getCloudVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
use OCP\Util;
35
36
class MiscService {
37
38
	const ALPHA = 'abcdefghijklmnopqrstuvwxyz';
39
	const ALPHA_NUMERIC = 'abcdefghijklmnopqrstuvwxyz0123456789';
40
	const ALPHA_NUMERIC_SCORES = 'abcdefghijklmnopqrstuvwxyz0123456789_-';
41
42
	/** @var ILogger */
43
	private $logger;
44
45
	public function __construct(ILogger $logger) {
46
		$this->logger = $logger;
47
	}
48
49
	/**
50
	 * @param string $message
51
	 * @param int $level
52
	 */
53
	public function log($message, $level = 2) {
54
		$data = array(
55
			'app'   => Application::APP_NAME,
56
			'level' => $level
57
		);
58
59
		$this->logger->log($level, $message, $data);
60
	}
61
62
63
	/**
64
	 * @param string $path
65
	 *
66
	 * @return string
67
	 */
68
	public static function endSlash($path) {
69
		if ($path === '') {
70
			return '';
71
		}
72
73
		if (substr($path, -1, 1) !== '/') {
74
			$path .= '/';
75
		}
76
77
		return $path;
78
	}
79
80
81
82
83
	/**
84
	 * @param $arr
85
	 * @param $k
86
	 *
87
	 * @param string $default
88
	 *
89
	 * @return array|string|integer
90
	 */
91
	public static function get($arr, $k, $default = '') {
92
		if (!key_exists($k, $arr)) {
93
			return $default;
94
		}
95
96
		return $arr[$k];
97
	}
98
99
100
	public static function mustContains($data, $arr) {
101
		if (!is_array($arr)) {
102
			$arr = [$arr];
103
		}
104
105
		foreach ($arr as $k) {
106
			if (!key_exists($k, $data)) {
107
				throw new MissingKeyInArrayException('missing_key_in_array');
108
			}
109
		}
110
	}
111
112
113
114
115
116
	public static function checkChars($line, $chars) {
117
		for ($i = 0; $i < strlen($line); $i++) {
118
			if (strpos($chars, substr($line, $i, 1)) === false) {
119
				return false;
120
			}
121
		}
122
123
		return true;
124
	}
125
126
	/**
127
	 * @param array $data
128
	 *
129
	 * @return DataResponse
130
	 */
131
	public function fail($data) {
132
		$this->log(json_encode($data));
133
134
		return new DataResponse(
135
			array_merge($data, array('status' => 0)),
136
			Http::STATUS_NON_AUTHORATIVE_INFORMATION
137
		);
138
	}
139
140
141
	/**
142
	 * @param array $data
143
	 *
144
	 * @return DataResponse
145
	 */
146
	public function success($data) {
147
		return new DataResponse(
148
			array_merge($data, array('status' => 1)),
149
			Http::STATUS_CREATED
150
		);
151
	}
152
153
154
	/**
155
	 * return the cloud version.
156
	 * if $complete is true, return a string x.y.z
157
	 *
158
	 * @param boolean $complete
159
	 *
160
	 * @return string|integer
161
	 */
162 View Code Duplication
	public function getCloudVersion($complete = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
		$ver = Util::getVersion();
164
165
		if ($complete) {
166
			return implode('.', $ver);
167
		}
168
169
		return $ver[0];
170
	}
171
172
173
}
174
175