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

Utils   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 26
lcom 0
cbo 1
dl 0
loc 138
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B recurseDelete() 0 28 7
A absoluteUrl() 0 4 1
A getPublicUrl() 0 9 2
A mbUcfirst() 0 4 1
B parseBytes() 0 27 9
B showBytes() 0 20 6
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
/**
15
 * Utils class.
16
 */
17
class Utils
18
{
19
	public static function recurseDelete(string $src)
20
	{
21
		$vendorDir = \dirname(__FILE__, 2);
22
		$rootDir = \dirname($vendorDir, 2) . \DIRECTORY_SEPARATOR;
23
		if (!file_exists($rootDir . $src)) {
24
			return;
25
		}
26
		$dirs = [];
27
		if (is_dir($src)) {
28
			$dirs[] = $rootDir . $src;
29
		}
30
		@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...
31
		if (is_dir($src)) {
32
			foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
33
				if ($item->isDir()) {
34
					$dirs[] = $rootDir . $src . \DIRECTORY_SEPARATOR . $iterator->getSubPathName();
35
				} else {
36
					unlink($rootDir . $src . \DIRECTORY_SEPARATOR . $iterator->getSubPathName());
37
				}
38
			}
39
			arsort($dirs);
40
			foreach ($dirs as $dir) {
41
				rmdir($dir);
42
			}
43
		} else {
44
			unlink($rootDir . $src);
45
		}
46
	}
47
48
	/**
49
	 * Get absolute URL for Portal2.
50
	 *
51
	 * @param string $url
52
	 *
53
	 * @return string
54
	 */
55
	public static function absoluteUrl(string $url): string
56
	{
57
		return \App\Config::get('portalUrl') . $url;
58
	}
59
60
	/**
61
	 * Get public url from file.
62
	 *
63
	 * @param string $name
64
	 * @param bool   $full
65
	 *
66
	 * @return string
67
	 */
68
	public static function getPublicUrl($name, $full = false): string
69
	{
70
		$basePath = '';
71
		if ($full) {
72
			$basePath .= \App\Config::get('portalUrl');
73
		}
74
		$basePath .= PUBLIC_DIRECTORY;
75
		return $basePath . $name;
76
	}
77
78
	/**
79
	 * Replacement for the ucfirst function for proper Multibyte String operation.
80
	 * Delete function will exist as mb_ucfirst.
81
	 *
82
	 * @param string $string
83
	 *
84
	 * @return string
85
	 */
86
	public static function mbUcfirst(string $string): string
87
	{
88
		return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
89
	}
90
91
	/**
92
	 * Parse bytes.
93
	 *
94
	 * @param mixed $str
95
	 *
96
	 * @return float
97
	 */
98
	public static function parseBytes($str): float
99
	{
100
		if (is_numeric($str)) {
101
			return (float) $str;
102
		}
103
		$bytes = 0;
104
		if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) {
105
			$bytes = (float) ($regs[1]);
106
			switch (strtolower($regs[2])) {
107
				case 'g':
108
				case 'gb':
109
					$bytes *= 1073741824;
110
					break;
111
				case 'm':
112
				case 'mb':
113
					$bytes *= 1048576;
114
					break;
115
				case 'k':
116
				case 'kb':
117
					$bytes *= 1024;
118
					break;
119
				default:
120
					break;
121
			}
122
		}
123
		return (float) $bytes;
124
	}
125
126
	/**
127
	 * Show bytes.
128
	 *
129
	 * @param mixed       $bytes
130
	 * @param string|null $unit
131
	 *
132
	 * @return string
133
	 */
134
	public static function showBytes($bytes, &$unit = null): string
135
	{
136
		$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...
137
		if ($bytes >= 1073741824) {
138
			$unit = 'GB';
139
			$gb = $bytes / 1073741824;
140
			$str = sprintf($gb >= 10 ? '%d ' : '%.2f ', $gb) . $unit;
141
		} elseif ($bytes >= 1048576) {
142
			$unit = 'MB';
143
			$mb = $bytes / 1048576;
144
			$str = sprintf($mb >= 10 ? '%d ' : '%.2f ', $mb) . $unit;
145
		} elseif ($bytes >= 1024) {
146
			$unit = 'KB';
147
			$str = sprintf('%d ', round($bytes / 1024)) . $unit;
148
		} else {
149
			$unit = 'B';
150
			$str = sprintf('%d ', $bytes) . $unit;
151
		}
152
		return $str;
153
	}
154
}
155