Test Setup Failed
Pull Request — developer (#252)
by Arkadiusz
05:46
created

Utils::recurseDelete()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
cc 7
nc 15
nop 1
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(\dirname(__FILE__));
22
		$rootDir = \dirname(\dirname($vendorDir)) . \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