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

Helpers::writeFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
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