Test Setup Failed
Pull Request — developer (#294)
by Arkadiusz
34:26
created

Utils::showBytes()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
cc 6
nc 4
nop 2
1
<?php
2
/**
3
 * The file contains: Utils class.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App;
13
14
\define('IS_PUBLIC_DIR', true);
15
/**
16
 * Utils class.
17
 */
18
class Utils
19
{
20
	public static function recurseDelete(string $src)
21
	{
22
		$vendorDir = \dirname(__FILE__, 2);
23
		$rootDir = \dirname($vendorDir, 2) . \DIRECTORY_SEPARATOR;
24
		if (!file_exists($rootDir . $src)) {
25
			return;
26
		}
27
		$dirs = [];
28
		if (is_dir($src)) {
29
			$dirs[] = $rootDir . $src;
30
		}
31
		@chmod($rootDir . $src, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
32
		if (is_dir($src)) {
33
			foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
34
				if ($item->isDir()) {
35
					$dirs[] = $rootDir . $src . \DIRECTORY_SEPARATOR . $iterator->getSubPathName();
36
				} else {
37
					unlink($rootDir . $src . \DIRECTORY_SEPARATOR . $iterator->getSubPathName());
38
				}
39
			}
40
			arsort($dirs);
41
			foreach ($dirs as $dir) {
42
				rmdir($dir);
43
			}
44
		} else {
45
			unlink($rootDir . $src);
46
		}
47
	}
48
49
	/**
50
	 * Get absolute URL for Portal2.
51
	 *
52
	 * @param string $url
53
	 *
54
	 * @return string
55
	 */
56
	public static function absoluteUrl(string $url): string
57
	{
58
		return \App\Config::get('portalUrl') . $url;
59
	}
60
61
	/**
62
	 * Get public url from file.
63
	 *
64
	 * @param string $name
65
	 * @param bool   $full
66
	 *
67
	 * @return string
68
	 */
69
	public static function getPublicUrl($name, $full = false): string
70
	{
71
		$basePath = '';
72
		if ($full) {
73
			$basePath .= \App\Config::get('portalUrl');
74
		}
75
		if (!IS_PUBLIC_DIR) {
76
			$basePath .= 'public_html/';
77
		}
78
		return $basePath . $name;
79
	}
80
81
	/**
82
	 * Replacement for the ucfirst function for proper Multibyte String operation.
83
	 * Delete function will exist as mb_ucfirst.
84
	 *
85
	 * @param string $string
86
	 *
87
	 * @return string
88
	 */
89
	public static function mbUcfirst(string $string): string
90
	{
91
		return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
92
	}
93
94
	/**
95
	 * Parse bytes.
96
	 *
97
	 * @param mixed $str
98
	 *
99
	 * @return float
100
	 */
101
	public static function parseBytes($str): float
102
	{
103
		if (is_numeric($str)) {
104
			return (float) $str;
105
		}
106
		$bytes = 0;
107
		if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) {
108
			$bytes = (float) ($regs[1]);
109
			switch (strtolower($regs[2])) {
110
				case 'g':
111
				case 'gb':
112
					$bytes *= 1073741824;
113
					break;
114
				case 'm':
115
				case 'mb':
116
					$bytes *= 1048576;
117
					break;
118
				case 'k':
119
				case 'kb':
120
					$bytes *= 1024;
121
					break;
122
				default:
123
					break;
124
			}
125
		}
126
		return (float) $bytes;
127
	}
128
129
	/**
130
	 * Show bytes.
131
	 *
132
	 * @param mixed       $bytes
133
	 * @param string|null $unit
134
	 *
135
	 * @return string
136
	 */
137
	public static function showBytes($bytes, &$unit = null): string
138
	{
139
		$bytes = self::parseBytes($bytes);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $bytes. This often makes code more readable.
Loading history...
140
		if ($bytes >= 1073741824) {
141
			$unit = 'GB';
142
			$gb = $bytes / 1073741824;
143
			$str = sprintf($gb >= 10 ? '%d ' : '%.2f ', $gb) . $unit;
144
		} elseif ($bytes >= 1048576) {
145
			$unit = 'MB';
146
			$mb = $bytes / 1048576;
147
			$str = sprintf($mb >= 10 ? '%d ' : '%.2f ', $mb) . $unit;
148
		} elseif ($bytes >= 1024) {
149
			$unit = 'KB';
150
			$str = sprintf('%d ', round($bytes / 1024)) . $unit;
151
		} else {
152
			$unit = 'B';
153
			$str = sprintf('%d ', $bytes) . $unit;
154
		}
155
		return $str;
156
	}
157
}
158