Passed
Push — issue-14 ( 6b772e )
by Koldo
04:23
created

CommonFileStructure   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 84
ccs 25
cts 27
cp 0.9259
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A verifyInstallationPath() 0 8 5
A getContainer() 0 17 1
A createDirectories() 0 5 4
A createFiles() 0 8 3
A removeCommunityFiles() 0 6 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Installer\Template;
6
7
use RuntimeException;
8
9
use function chmod;
10
use function file_exists;
11
use function file_put_contents;
12
use function in_array;
13
use function is_dir;
14
use function is_writable;
15
use function mkdir;
16
use function sprintf;
17
use function unlink;
18
19
abstract class CommonFileStructure implements FileStructureFactory
20
{
21
    public const COMMUNITY_FILES = [
22
        '/CHANGELOG.md',
23
        '/CODE_OF_CONDUCT.md',
24
        '/CONTRIBUTING.md',
25
        '/PULL_REQUEST_TEMPLATE.md',
26
        '/LICENSE',
27
    ];
28
29
    public const EXECUTABLES = [
30
        'bin/console',
31
    ];
32
33
    public const NOT_WRITABLE_MESSAGE = 'Given directory "%s" is not writable.';
34
    public const NOT_PERMISSIONS_MESSAGE = 'Directory "%s" was not created by permission issues.';
35
36 6
    protected function verifyInstallationPath(string $installationPath): void
37
    {
38 6
        if (!is_dir($installationPath) && !mkdir($dir = $installationPath, 0755, true) && !is_dir($dir)) {
39 1
            throw new RuntimeException(sprintf(self::NOT_PERMISSIONS_MESSAGE, $dir));
40
        }
41
42 5
        if (!is_writable($installationPath)) {
43 1
            throw new RuntimeException(sprintf(self::NOT_WRITABLE_MESSAGE, $installationPath));
44
        }
45 4
    }
46
47
    /**
48
     * @param string $installationPath
49
     * @param array<string> $directories
50
     */
51 4
    protected function createDirectories(string $installationPath, array $directories): void
52
    {
53 4
        foreach ($directories as $directory) {
54 4
            if (!mkdir($dir = sprintf('%s/%s', $installationPath, $directory), 0755, true) && !is_dir($dir)) {
55
                throw new RuntimeException(sprintf('Directory "%s" was not created', $dir));
56
            }
57
        }
58 4
    }
59
60
    /**
61
     * @param string $installationPath
62
     * @param array<string, string> $files
63
     */
64 4
    protected function createFiles(string $installationPath, array $files): void
65
    {
66 4
        foreach ($files as $method => $filename) {
67 4
            $file = sprintf('%s/%s', $installationPath, $filename);
68 4
            file_put_contents($file, $this->$method());
69
70 4
            if (in_array($filename, self::EXECUTABLES, true)) {
71
                chmod($file, 755);
72
            }
73
        }
74 4
    }
75
76 2
    protected function removeCommunityFiles(string $installationPath): void
77
    {
78 2
        foreach (self::COMMUNITY_FILES as $fileToDelete) {
79 2
            $filePath = $installationPath . $fileToDelete;
80 2
            if (file_exists($filePath)) {
81 1
                unlink($filePath);
82
            }
83
        }
84 2
    }
85
86 2
    public static function getContainer(): string
87
    {
88
        $containerContent = <<<'PHP'
89 2
<?php
90
91
declare(strict_types=1);
92
93
// Load configuration
94
use Antidot\Container\Builder;
95
96
$config = require __DIR__ . '/../config/config.php';
97
98
return Builder::build($config, true);
99
100
PHP;
101
102 2
        return $containerContent;
103
    }
104
}
105