Completed
Push — master ( 184857...687c8c )
by Anton
05:59
created

Plugin::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
nop 0
1
<?php
2
/**
3
 * Bluz composer plugin
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/composer-plugin
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Composer\Installers;
13
14
use Composer\Composer;
15
use Composer\EventDispatcher\EventSubscriberInterface;
16
use Composer\IO\IOInterface;
17
use Composer\Plugin\PluginInterface;
18
use Composer\Script\ScriptEvents;
19
20
class Plugin implements PluginInterface, EventSubscriberInterface
21
{
22
    const PERMISSION_CODE = 0755;
23
    const REPEAT = 5;
24
25
    /**
26
     * @var Installer
27
     */
28
    protected $installer;
29
30
    /**
31
     * @var string
32
     */
33
    protected $environment;
34
35
    /**
36
     * Create instance, define constants
37
     */
38
    public function __construct()
39
    {
40
        defined('PATH_ROOT') ? : define('PATH_ROOT', realpath($_SERVER['DOCUMENT_ROOT']));
41
        defined('DS') ? : define('DS', DIRECTORY_SEPARATOR);
42
    }
43
44
    /**
45
     * Called after the plugin is loaded
46
     *
47
     * It setup composer installer
48
     *
49
     * {@inheritDoc}
50
     */
51
    public function activate(Composer $composer, IOInterface $io)
52
    {
53
        $this->installer = new Installer($io, $composer);
54
        $composer->getInstallationManager()->addInstaller($this->installer);
55
    }
56
57
    /**
58
     * Registered events after the plugin is loaded
59
     *
60
     * {@inheritDoc}
61
     */
62
    public static function getSubscribedEvents(): array
63
    {
64
        $result = [
65
            // copy files to working directory
66
            ScriptEvents::POST_PACKAGE_INSTALL => [
0 ignored issues
show
Deprecated Code introduced by
The constant Composer\Script\ScriptEvents::POST_PACKAGE_INSTALL has been deprecated with message: Use Composer\Installer\PackageEvents::POST_PACKAGE_INSTALL instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
67
                ['onPostPackageInstall', 0]
68
            ],
69
            // check changes
70
            ScriptEvents::PRE_PACKAGE_UPDATE  => [
0 ignored issues
show
Deprecated Code introduced by
The constant Composer\Script\ScriptEvents::PRE_PACKAGE_UPDATE has been deprecated with message: Use Composer\Installer\PackageEvents::PRE_PACKAGE_UPDATE instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
71
                ['onPrePackageUpdate', 0]
72
            ],
73
            // if working files is changed skip this step
74
            // else copy files to working directory
75
            ScriptEvents::POST_PACKAGE_UPDATE  => [
0 ignored issues
show
Deprecated Code introduced by
The constant Composer\Script\ScriptEvents::POST_PACKAGE_UPDATE has been deprecated with message: Use Composer\Installer\PackageEvents::POST_PACKAGE_UPDATE instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
76
                ['onPostPackageUpdate', 0]
77
            ],
78
            // removed all files
79
            ScriptEvents::PRE_PACKAGE_UNINSTALL  => [
0 ignored issues
show
Deprecated Code introduced by
The constant Composer\Script\ScriptEv...::PRE_PACKAGE_UNINSTALL has been deprecated with message: Use Composer\Installer\PackageEvents::PRE_PACKAGE_UNINSTALL instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
80
                ['onPrePackageUninstall', 0]
81
            ]
82
        ];
83
84
        return $result;
85
    }
86
87
    /**
88
     * Hook which is called after install package
89
     *
90
     * It copies bluz module
91
     */
92
    public function onPostPackageInstall()
93
    {
94
        $this->copy();
95
    }
96
97
    /**
98
     * Hook which is called before update package
99
     *
100
     * It copies bluz module
101
     */
102
    public function onPrePackageUpdate()
103
    {
104
        $this->check();
105
    }
106
107
    /**
108
     * Hook which is called after update package
109
     *
110
     * It copies bluz module
111
     */
112
    public function onPostPackageUpdate()
113
    {
114
        $this->copy();
115
    }
116
117
    /**
118
     * Hook which is called before remove package
119
     *
120
     * It copies bluz module
121
     */
122
    public function onPrePackageRemove()
123
    {
124
        $this->remove();
125
    }
126
127
    /**
128
     * It recursively copies the files and directories
129
     * @return bool
130
     */
131
    protected function copy()
132
    {
133
        foreach ($iterator = new \RecursiveIteratorIterator(
134
            new \RecursiveDirectoryIterator(
135
                $this->installer->getOption('vendorPath'),
136
                \RecursiveDirectoryIterator::SKIP_DOTS
137
            ),
138
            \RecursiveIteratorIterator::SELF_FIRST
139
        ) as $item) {
140
            $filePath = PATH_ROOT . DS . $iterator->getSubPathName();
141
142
            if (file_exists($filePath)) {
143
                $this->installer->getIo()->write("File {$iterator->getSubPathName()} already exists");
144
            } else {
145
                if ($item->isDir()) {
146
                    mkdir($filePath, self::PERMISSION_CODE);
147
                } else {
148
                    copy($item, $filePath);
149
                }
150
            }
151
        }
152
        return true;
153
    }
154
155
    /**
156
     * It recursively check changes in the files
157
     * @return bool
158
     */
159 View Code Duplication
    protected function check()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        foreach ($iterator = new \RecursiveIteratorIterator(
162
            new \RecursiveDirectoryIterator(
163
                $this->installer->getOption('vendorPath'),
164
                \RecursiveDirectoryIterator::SKIP_DOTS
165
            ),
166
            \RecursiveIteratorIterator::SELF_FIRST
167
        ) as $item) {
168
            $this->installer->getIo()->write($iterator->getSubPathName());
169
            $this->installer->getIo()->write(PATH_ROOT . DS . $iterator->getSubPathName());
170
171
        }
172
173
        return false;
174
    }
175
176
    /**
177
     * It recursively removes the files and directories
178
     * @return bool
179
     */
180 View Code Duplication
    protected function remove()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    {
182
        foreach ($iterator = new \RecursiveIteratorIterator(
183
            new \RecursiveDirectoryIterator(
184
                $this->installer->getOption('vendorPath'),
185
                \RecursiveDirectoryIterator::SKIP_DOTS
186
            ),
187
            \RecursiveIteratorIterator::SELF_FIRST
188
        ) as $item) {
189
            $this->installer->getIo()->write($iterator->getSubPathName());
190
            $this->installer->getIo()->write(PATH_ROOT . DS . $iterator->getSubPathName());
191
192
        }
193
194
        return false;
195
    }
196
}
197