Passed
Push — master ( b9addd...bd3c1c )
by Enjoys
01:53
created

Helpers   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 22
c 2
b 0
f 1
dl 0
loc 59
ccs 25
cts 25
cp 1
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getHttpScheme() 0 15 6
A createDirectory() 0 18 4
A writeFile() 0 6 2
1
<?php
2
3
namespace Enjoys\AssetsCollector;
4
5
class Helpers
6
{
7
8 15
    public static function getHttpScheme(): string
9
    {
10 15
        $scheme = 'http';
11 15
        if (isset($_SERVER['HTTP_SCHEME'])) {
12 1
            return $_SERVER['HTTP_SCHEME'];
13
        }
14
15 14
        if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') {
16 1
            return 'https';
17
        }
18
19 13
        if (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT']) {
20 1
            return 'https';
21
        }
22 12
        return $scheme;
23
    }
24
25
    /**
26
     * @param string $file
27
     * @param string $data
28
     * @param string $mode
29
     * @return void
30
     */
31 2
    public static function writeFile(string $file, string $data, string $mode = 'w'): void
32
    {
33 2
        $f = fopen($file, $mode);
34 2
        if ($f !== false) {
35 2
            fwrite($f, $data);
36 2
            fclose($f);
37
        }
38 2
    }
39
40
    /**
41
     * @param string $path
42
     * @param int $permissions
43
     * @return void
44
     * @throws \Exception
45
     */
46 13
    public static function createDirectory(string $path, int $permissions = 0777): void
47
    {
48
49 13
        if (preg_match("/(\/\.+|\.+)$/i", $path)) {
50 6
            throw new \Exception(
51 6
                sprintf("Нельзя создать директорию: %s", $path)
52
            );
53
        }
54
55
        //Clear the most recent error
56 7
        error_clear_last();
57
58 7
        if (!is_dir($path)) {
59 7
            if (@mkdir($path, $permissions, true) === false) {
60
                /** @var string[] $error */
61 1
                $error = error_get_last();
62 1
                throw new \Exception(
63 1
                    sprintf("Не удалось создать директорию: %s! Причина: %s", $path, $error['message'])
64
                );
65
            }
66
        }
67 6
    }
68
}
69