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

Helpers   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 9
Bugs 1 Features 3
Metric Value
eloc 23
c 9
b 1
f 3
dl 0
loc 77
ccs 28
cts 30
cp 0.9333
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createSymlink() 0 10 3
A createDirectory() 0 6 2
A createEmptyFile() 0 5 1
A getHttpScheme() 0 15 6
A writeFile() 0 9 1
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