Passed
Push — master ( 1719c8...830018 )
by Evgenii
01:09
created

Plugin::install()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helick\CMSInstaller;
4
5
use Composer\Composer;
6
use Composer\EventDispatcher\EventSubscriberInterface;
7
use Composer\IO\IOInterface;
8
use Composer\Plugin\PluginInterface;
9
10
final class Plugin implements PluginInterface, EventSubscriberInterface
11
{
12
    /**
13
     * @inheritDoc
14
     */
15
    public static function getSubscribedEvents()
16
    {
17
        return [
18
            'post-install-cmd' => ['install'],
19
            'post-update-cmd'  => ['install'],
20
        ];
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public function activate(Composer $composer, IOInterface $io)
27
    {
28
        //
29
    }
30
31
    /**
32
     * Kick-off the installation.
33
     *
34
     * @return void
35
     */
36
    public function install(): void
37
    {
38
        $sourceDir = dirname(__DIR__, 1) . '/resources/stubs';
39
        $destDir   = dirname(__DIR__, 4);
40
41
        $this->createDirectories($destDir);
42
43
        $this->copyFiles($sourceDir, $destDir);
44
    }
45
46
    /**
47
     * Create necessary directories.
48
     *
49
     * @param string $destDir
50
     *
51
     * @return void
52
     */
53
    private function createDirectories(string $destDir): void
54
    {
55
        $directories = [
56
            '/bootstrap',
57
            '/bootstrap/cache',
58
            '/config',
59
            '/config/environments',
60
            '/web',
61
            '/web/content',
62
            '/web/content/mu-plugins',
63
            '/web/content/plugins',
64
            '/web/content/themes',
65
            '/web/content/uploads',
66
        ];
67
68
        $directories = array_filter($directories, function ($directory) use ($destDir) {
69
            return !is_dir($destDir . $directory);
70
        });
71
72
        foreach ($directories as $directory) {
73
            mkdir($destDir . $directory);
74
        }
75
    }
76
77
    /**
78
     * Copy necessary files.
79
     *
80
     * @param string $sourceDir
81
     * @param string $destDir
82
     *
83
     * @return void
84
     */
85
    private function copyFiles(string $sourceDir, string $destDir): void
86
    {
87
        $files = [
88
            '/bootstrap/cache/mu-plugins.php',
89
            '/bootstrap/cache/required-mu-plugins.php',
90
            '/config/environments/development.php',
91
            '/config/environments/staging.php',
92
            '/config/application.php',
93
            '/web/content/mu-plugins/autoloader.php',
94
            '/web/index.php',
95
            '/web/wp-config.php',
96
            '/.gitattributes',
97
            '/.gitignore',
98
            '/.env.example',
99
            '/wp-cli.yml',
100
        ];
101
102
        $files = array_filter($files, function ($file) use ($destDir) {
103
            return !file_exists($destDir . $file);
104
        });
105
106
        foreach ($files as $file) {
107
            copy($sourceDir . $file . '.stub', $destDir . $file);
108
        }
109
    }
110
}
111