Passed
Push — master ( b328e0...054509 )
by Enjoys
02:07
created

Helpers::createSymlink()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 7
Bugs 1 Features 2
Metric Value
cc 3
eloc 6
c 7
b 1
f 2
nc 4
nop 3
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 10
1
<?php
2
3
namespace Enjoys\AssetsCollector;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
8
use function Enjoys\FileSystem\createDirectory;
9
use function Enjoys\FileSystem\CreateSymlink;
10
use function Enjoys\FileSystem\writeFile;
11
12
class Helpers
13
{
14
15 15
    public static function getHttpScheme(): string
16
    {
17 15
        $scheme = 'http';
18 15
        if (isset($_SERVER['HTTP_SCHEME'])) {
19 1
            return $_SERVER['HTTP_SCHEME'];
20
        }
21
22 14
        if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') {
23 1
            return 'https';
24
        }
25
26 13
        if (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT']) {
27 1
            return 'https';
28
        }
29 12
        return $scheme;
30
    }
31
32
    /**
33
     * @param string $file
34
     * @param string $data
35
     * @param string $mode
36
     * @param LoggerInterface|null $logger
37
     * @return void
38
     */
39 1
    public static function writeFile(
40
        string $file,
41
        string $data,
42
        string $mode = 'w',
43
        LoggerInterface $logger = null
44
    ): void {
45 1
        $logger ??= new NullLogger();
46 1
        writeFile($file, $data, $mode);
47 1
        $logger->info(sprintf('Write to: %s', $file));
48 1
    }
49
50 2
    public static function createEmptyFile(string $file, LoggerInterface $logger = null): void
51
    {
52 2
        $logger ??= new NullLogger();
53 2
        writeFile($file, '');
54 2
        $logger->info(sprintf('Create file: %s', $file));
55 2
    }
56
57
    /**
58
     * @param string $path
59
     * @param int $permissions
60
     * @param LoggerInterface|null $logger
61
     * @return void
62
     * @throws \Exception
63
     */
64 10
    public static function createDirectory(string $path, int $permissions = 0777, LoggerInterface $logger = null): void
65
    {
66 10
        $logger ??= new NullLogger();
67
68 10
        if (createDirectory($path, $permissions)) {
69 4
            $logger->info(sprintf('Create directory %s', $path));
70
        }
71 4
    }
72
73
    /**
74
     * @param string $link
75
     * @param string $target
76
     * @param LoggerInterface|null $logger
77
     * @throws \Exception
78
     */
79 11
    public static function createSymlink(string $link, string $target, LoggerInterface $logger = null): void
80
    {
81 11
        $logger ??= new NullLogger();
82
83
        try {
84 11
            if (CreateSymlink($link, $target)) {
85 11
                $logger->info(sprintf('Created symlink: %s', $link));
86
            }
87
        } catch (\Exception $e) {
88
            $logger->notice($e->getMessage());
89
        }
90 11
    }
91
}
92