Utils   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 91
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTime() 0 3 1
A getMicroTime() 0 3 1
A getNameByUid() 0 4 1
A splitContent() 0 15 3
A getItemByProperty() 0 8 3
A GUID() 0 8 2
A getUserInfo() 0 14 2
1
<?php
2
/**
3
 * Nextcloud - namespace OCA\Nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Utility;
25
26
class Utils {
27
    /**
28
     * Gets the unix epoch UTC timestamp
29
     * @return int
30
     */
31
    public static $maxPartSize = 2621440;
32
33
	public static function getTime() {
34
		return (new \DateTime())->getTimestamp();
35
	}
36
	/**
37
	 * @return int the current unix time in milliseconds
38
	 */
39
	public static function getMicroTime() {
40
		return microtime(true);
41
	}
42
43
	/**
44
	 * @param $uid
45
	 * @return string
46
	 */
47
	public static function getNameByUid($uid){
48
		$u = Utils::getUserInfo($uid);
49
		return $u['display_name'];
50
	}
51
52
	public static function getUserInfo($uid){
53
		$um = \OC::$server->getUserManager();
54
		$u = $um->get($uid);
55
		if($u) {
56
			$user = [
57
				'display_name' => $u->getDisplayName(),
58
				'uid' => $uid,
59
				'avatar' => $u->getAvatarImage(32)
60
			];
61
		} else {
62
			return false;
63
		}
64
		return $user;
65
	}
66
67
	/**
68
	 * Splits a string in parts of 5Mb
69
	 * @param $str
70
	 * @return array
71
	 */
72
	public function splitContent($str) {
73
		$maxlength = Utils::$maxPartSize; // 5 Megs (2 bytes per character)
74
		$count = 0;
75
		$strarray = array();
76
		while (true) {
77
			if (strlen($str) <= $maxlength) {
78
				$strarray[$count++] = $str;
79
				return $strarray;
80
			} else {
81
				$strarray[$count++] = substr($str, 0, $maxlength);
82
				$str = substr($str, $maxlength);
83
			}
84
		}
85
		return $strarray;
86
	}
87
88
	/**
89
	 * @param $key
90
	 * @param $value
91
	 * @param $array
92
	 * @return int|null|string
93
	 * @internal param $ $
94
	 */
95
	public static function getItemByProperty($key, $value, $array) {
96
		foreach ($array as $_key => $val) {
97
			if ($val[$key] === $value) {
98
				return $val;
99
			}
100
		}
101
		return null;
102
	}
103
104
	/**
105
	 * Generates a Globally Unique ID
106
	 * @return string
107
	 */
108
	public static function GUID() {
109
		if (function_exists('com_create_guid') === true)
110
		{
111
			return trim(com_create_guid(), '{}');
112
		}
113
114
		return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
115
	}
116
}