Passed
Push — developer ( a9af3c...f6f186 )
by Mariusz
35:26
created

Utils   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 0
cbo 1
dl 0
loc 120
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B recurseDelete() 0 28 7
A absoluteUrl() 0 4 1
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
	 * Replacement for the ucfirst function for proper Multibyte String operation.
62
	 * Delete function will exist as mb_ucfirst.
63
	 *
64
	 * @param string $string
65
	 *
66
	 * @return string
67
	 */
68
	public static function mbUcfirst(string $string): string
69
	{
70
		return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
71
	}
72
73
	/**
74
	 * Parse bytes.
75
	 *
76
	 * @param mixed $str
77
	 *
78
	 * @return float
79
	 */
80
	public static function parseBytes($str): float
81
	{
82
		if (is_numeric($str)) {
83
			return (float) $str;
84
		}
85
		$bytes = 0;
86
		if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) {
87
			$bytes = (float) ($regs[1]);
88
			switch (strtolower($regs[2])) {
89
				case 'g':
90
				case 'gb':
91
					$bytes *= 1073741824;
92
					break;
93
				case 'm':
94
				case 'mb':
95
					$bytes *= 1048576;
96
					break;
97
				case 'k':
98
				case 'kb':
99
					$bytes *= 1024;
100
					break;
101
				default:
102
					break;
103
			}
104
		}
105
		return (float) $bytes;
106
	}
107
108
	/**
109
	 * Show bytes.
110
	 *
111
	 * @param mixed       $bytes
112
	 * @param string|null $unit
113
	 *
114
	 * @return string
115
	 */
116
	public static function showBytes($bytes, &$unit = null): string
117
	{
118
		$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...
119
		if ($bytes >= 1073741824) {
120
			$unit = 'GB';
121
			$gb = $bytes / 1073741824;
122
			$str = sprintf($gb >= 10 ? '%d ' : '%.2f ', $gb) . $unit;
123
		} elseif ($bytes >= 1048576) {
124
			$unit = 'MB';
125
			$mb = $bytes / 1048576;
126
			$str = sprintf($mb >= 10 ? '%d ' : '%.2f ', $mb) . $unit;
127
		} elseif ($bytes >= 1024) {
128
			$unit = 'KB';
129
			$str = sprintf('%d ', round($bytes / 1024)) . $unit;
130
		} else {
131
			$unit = 'B';
132
			$str = sprintf('%d ', $bytes) . $unit;
133
		}
134
		return $str;
135
	}
136
}
137