Passed
Push — master ( bd3c1c...f4ac9e )
by Enjoys
02:18
created

Helpers::createEmptyFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Enjoys\AssetsCollector;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
8
class Helpers
9
{
10
11 15
    public static function getHttpScheme(): string
12
    {
13 15
        $scheme = 'http';
14 15
        if (isset($_SERVER['HTTP_SCHEME'])) {
15 1
            return $_SERVER['HTTP_SCHEME'];
16
        }
17
18 14
        if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') {
19 1
            return 'https';
20
        }
21
22 13
        if (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT']) {
23 1
            return 'https';
24
        }
25 12
        return $scheme;
26
    }
27
28
    /**
29
     * @param string $file
30
     * @param string $data
31
     * @param string $mode
32
     * @param LoggerInterface|null $logger
33
     * @return void
34
     */
35 1
    public static function writeFile(
36
        string $file,
37
        string $data,
38
        string $mode = 'w',
39
        LoggerInterface $logger = null
40
    ): void {
41 1
        $logger ??= new NullLogger();
42
43 1
        $f = fopen($file, $mode);
44 1
        if ($f !== false) {
45 1
            fwrite($f, $data);
46 1
            fclose($f);
47 1
            $logger->info(sprintf('Write to: %s', $file));
48
        }
49 1
    }
50
51 2
    public static function createEmptyFile(string $file, LoggerInterface $logger = null): void
52
    {
53 2
        $logger ??= new NullLogger();
54
55 2
        $f = fopen($file, 'w');
56 2
        if ($f !== false) {
57 2
            fwrite($f, '');
58 2
            fclose($f);
59 2
            $logger->info(sprintf('Create file: %s', $file));
60
        }
61 2
    }
62
63
    /**
64
     * @param string $path
65
     * @param int $permissions
66
     * @param LoggerInterface|null $logger
67
     * @return void
68
     * @throws \Exception
69
     */
70 13
    public static function createDirectory(string $path, int $permissions = 0777, LoggerInterface $logger = null): void
71
    {
72 13
        $logger ??= new NullLogger();
73
74 13
        if (preg_match("/(\/\.+|\.+)$/i", $path)) {
75 6
            throw new \Exception(
76 6
                sprintf("Нельзя создать директорию: %s", $path)
77
            );
78
        }
79
80
        //Clear the most recent error
81 7
        error_clear_last();
82
83 7
        if (!is_dir($path)) {
84 7
            if (@mkdir($path, $permissions, true) === false) {
85
                /** @var string[] $error */
86 1
                $error = error_get_last();
87 1
                throw new \Exception(
88 1
                    sprintf("Не удалось создать директорию: %s! Причина: %s", $path, $error['message'])
89
                );
90
            }
91 6
            $logger->info(sprintf('Create directory %s', $path));
92
        }
93 6
    }
94
95
    /**
96
     * @param string $link
97
     * @param string $target
98
     * @param LoggerInterface|null $logger
99
     * @throws \Exception
100
     */
101 2
    public static function createSymlink(string $link, string $target, LoggerInterface $logger = null): void
102
    {
103 2
        $logger ??= new NullLogger();
104
105 2
        $directory = pathinfo($link, PATHINFO_DIRNAME);
106 2
        Helpers::createDirectory($directory, 0755, $logger);
0 ignored issues
show
Bug introduced by
It seems like $directory can also be of type array; however, parameter $path of Enjoys\AssetsCollector\Helpers::createDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
        Helpers::createDirectory(/** @scrutinizer ignore-type */ $directory, 0755, $logger);
Loading history...
107
108 2
        if (file_exists($link)) {
109
            symlink($target, $link);
110
            $logger->info(sprintf('Created symlink: %s', $link));
111
        }
112 2
    }
113
}
114