Passed
Push — master ( d56c43...736831 )
by Josh
04:39
created

Orchestrator::removePackageConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
rs 9.9666
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: joshgulledge
5
 * Date: 9/3/18
6
 * Time: 11:33 AM
7
 */
8
9
namespace LCI\MODX\Orchestrator;
10
11
use LCI\Blend\Blender;
12
use LCI\Blend\Helpers\Files;
13
use LCI\MODX\Console\Console;
14
use LCI\MODX\Console\Helpers\VoidUserInteractionHandler;
15
16
class Orchestrator
17
{
18
    use Files;
0 ignored issues
show
Bug introduced by
The trait LCI\Blend\Helpers\Files requires the property $output which is not provided by LCI\MODX\Orchestrator\Orchestrator.
Loading history...
19
20
    /** @var \modX */
0 ignored issues
show
Bug introduced by
The type modX was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
    public static $modx;
22
23
    /** @var bool|Console */
24
    protected static $console = false;
25
26
    /** @var string */
27
    protected static $package_file;
28
29
    /** @var bool|array */
30
    protected static $packages = false;
31
32
    /**
33
     * @throws \LCI\Blend\Exception\MigratorException
34
     */
35
    public static function install()
36
    {
37
        $path = getenv('LCI_ORCHESTRATOR_MIGRATION_PATH');
38
        if (empty($path)) {
39
            $path = static::getPackagePath();
40
        }
41
42
        self::runMigrations('lci/orchestrator', ['blend_modx_migration_dir' => $path]);
43
    }
44
45
    /**
46
     * @throws \LCI\Blend\Exception\MigratorException
47
     */
48
    public static function uninstall()
49
    {
50
        /** @var \LCI\MODX\Console\Console $console */
51
        $console = static::getConsole();
52
        // 1. install BLend
53
        $handler = new VoidUserInteractionHandler();
54
55
        $path = getenv('LCI_ORCHESTRATOR_MIGRATION_PATH');
56
        if (empty($path)) {
57
            $path = static::getPackagePath();
58
        }
59
60
        $blender = new Blender($console->loadMODX(), $handler, ['blend_modx_migration_dir' => $path]);
61
62
        if (!$blender->isBlendInstalledInModx()) {
63
            $blender->install();
64
        }
65
66
        // 2. run Migrations ~ install & update
67
        $blender->runMigration('down');
68
69
        $blender->install('down');
70
    }
71
72
    /**
73
     * @param string $package ~ ex: lci/stockpile
74
     */
75
    public static function addDependantPackageToConfig($package)
76
    {
77
        static::savePackageConfig($package);
78
    }
79
80
    /**
81
     * @param string $package ~ ex: lci/stockpile
82
     * @TODO Review ~ this is called on via a post composer script set up in extra
83
     *
84
     */
85
    public static function copyAssets($package)
86
    {
87
        // public will copy into the MODX public root path
88
        // assets will keep the same pathing
89
        $package_path = self::getPackagePath($package, false);
90
91
        $console = static::getConsole();
92
        $config = $console->getConfig();
93
94
        $self = new Orchestrator();
95
        $self->setMode(0755);
96
97
        if (file_exists($package_path . 'public')) {
98
            $destination = MODX_BASE_PATH;
0 ignored issues
show
Bug introduced by
The constant LCI\MODX\Orchestrator\MODX_BASE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99
            if (isset($config['LCI_ORCHESTRATOR_PUBLIC_PATH']) && file_exists($config['LCI_ORCHESTRATOR_PUBLIC_PATH'])) {
100
                $destination = $config['LCI_ORCHESTRATOR_PUBLIC_PATH'];
101
            }
102
103
            $self->copyDirectory($package_path . 'public', $destination);
104
        }
105
106
        if (file_exists($package_path . 'assets')) {
107
            $destination = MODX_ASSETS_PATH;
0 ignored issues
show
Bug introduced by
The constant LCI\MODX\Orchestrator\MODX_ASSETS_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
108
            if (isset($config['LCI_ORCHESTRATOR_ASSETS_PATH']) && file_exists($config['LCI_ORCHESTRATOR_ASSETS_PATH'])) {
109
                $destination = $config['LCI_ORCHESTRATOR_ASSETS_PATH'];
110
            }
111
112
            $self->copyDirectory($package_path . 'assets', $destination);
113
        }
114
    }
115
116
    /**
117
     * @param string $project ~ a valid composer project like lci/blend
118
     * @param string $type
119
     *
120
     * @throws \LCI\Blend\Exception\MigratorException
121
     */
122
    public static function installComposerPackage($project, $type='master')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$type" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$type"; expected 1 but found 0
Loading history...
123
    {
124
        $path = static ::getPackagePath($project);
125
126
        self::savePackageConfig($project);
127
        self::copyAssets($project);
128
        self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'up', $type);
129
    }
130
131
    /**
132
     * @param string $project ~ a valid composer project like lci/blend
133
     * @param string $type
134
     * @throws \LCI\Blend\Exception\MigratorException
135
     */
136
    public static function updateComposerPackage($project, $type='master')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$type" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$type"; expected 1 but found 0
Loading history...
137
    {
138
        $path = static ::getPackagePath($project);
139
140
        self::savePackageConfig($project);
141
        self::copyAssets($project);
142
        self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'up', $type);
143
    }
144
145
    /**
146
     * @throws \LCI\Blend\Exception\MigratorException
147
     */
148
    public static function updateAllOrchestratorComposerPackages()
149
    {
150
        static::loadOrchestratorPackageInfo();
151
152
        foreach(static::$packages as $existing_package) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
153
            static::updateComposerPackage($existing_package);
154
        }
155
    }
156
157
    /**
158
     * @param string $project ~ a valid composer project like lci/blend
159
     * @param string $type
160
     * @throws \LCI\Blend\Exception\MigratorException
161
     */
162
    public static function uninstallComposerPackage($project, $type='master')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$type" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$type"; expected 1 but found 0
Loading history...
163
    {
164
        $path = static ::getPackagePath($project);
165
166
        self::savePackageConfig($project);
167
        // @TODO remove assets
168
        self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'down', $type);
169
    }
170
171
    /**
172
     * @param string $project
173
     * @param array $config
174
     * @param string $method
175
     * @param string $type
176
     *
177
     * @throws \LCI\Blend\Exception\MigratorException
178
     */
179
    protected static function runMigrations($project, $config=[], $method='up', $type='master')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$config" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$config"; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$method" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$method"; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$type" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$type"; expected 1 but found 0
Loading history...
180
    {
181
        /** @var \LCI\MODX\Console\Console $console */
182
        $console = static::getConsole();
183
        static::$modx = $console->loadMODX();
184
185
        // 1. install BLend
186
        $handler = new VoidUserInteractionHandler();
187
188
        $blender = new Blender(static::$modx, $handler, $config);
189
190
        if (!$blender->isBlendInstalledInModx()) {
191
            $blender->install();
192
        }
193
194
        $blender->setProject($project);
195
196
        // 2. run Migrations
197
        $blender->runMigration($method, $type);
198
    }
199
200
    /**
201
     * @param string $package
202
     * @param bool $include_src
203
     * @return string
204
     */
205
    protected static function getPackagePath($package='lci/orchestrator', $include_src = true)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$package" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$package"; expected 1 but found 0
Loading history...
206
    {
207
        if (empty(static::$modx)) {
208
            /** @var \LCI\MODX\Console\Console $console */
209
            $console = static::getConsole();
210
            static::$modx = $console->loadMODX();
211
        }
212
213
        $path = static::$modx->getOption(
214
            'orchestrator.vendor_path',
215
            null,
216
            (defined('MODX_CORE_PATH') ? MODX_CORE_PATH.'vendor/' : dirname(__DIR__))
0 ignored issues
show
Bug introduced by
The constant LCI\MODX\Orchestrator\MODX_CORE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
217
        );
218
        $path .= $package . DIRECTORY_SEPARATOR;
219
220
        if ($include_src) {
221
            $path .= 'src' . DIRECTORY_SEPARATOR;
222
        }
223
224
        return $path;
225
    }
226
227
    /**
228
     * @return \LCI\MODX\Console\Console
229
     */
230
    protected static function getConsole()
231
    {
232
        if (!static::$console) {
233
            /** @var \LCI\MODX\Console\Console $console */
234
            static::$console = new Console();
235
        }
236
237
        return static::$console;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::console also could return the type true which is incompatible with the documented return type LCI\MODX\Console\Console.
Loading history...
238
    }
239
240
    /**
241
     *
242
     */
243
    protected static function loadOrchestratorPackageInfo()
244
    {
245
        /** @var \LCI\MODX\Console\Console $console */
246
        $console = static::getConsole();
247
248
        if (empty(static::$modx)) {
249
            static::$modx = $console->loadMODX();
250
        }
251
252
        if (!static::$packages) {
253
            static::$packages = [];
254
255
            static::$package_file = $console->getConfigFilePaths()['config_dir'] . 'lci_orchestrator_package.php';
256
257
            if (file_exists(static::$package_file)) {
258
                static::$packages = include static::$package_file;
259
            }
260
        }
261
    }
262
263
    /**
264
     * @param string $package
265
     */
266
    protected static function removePackageConfig($package)
267
    {
268
        static::loadOrchestratorPackageInfo();
269
        if (in_array($package, static::$packages)) {
0 ignored issues
show
Bug introduced by
It seems like static::packages can also be of type boolean; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

269
        if (in_array($package, /** @scrutinizer ignore-type */ static::$packages)) {
Loading history...
270
            $temp = [];
271
            foreach(static::$packages as $existing_package) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
272
                if ($existing_package == $package) {
273
                    continue;
274
                }
275
276
                $temp[] = $existing_package;
277
            }
278
            static::$packages = $temp;
279
280
            static::writeCacheFile(static::$package_file, static::$packages);
281
        }
282
    }
283
284
    /**
285
     * @param string $package
286
     */
287
    protected static function savePackageConfig($package)
288
    {
289
        static::loadOrchestratorPackageInfo();
290
        if (!in_array($package, static::$packages) && file_exists(static::getPackagePath($package, false))) {
0 ignored issues
show
Bug introduced by
It seems like static::packages can also be of type boolean; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

290
        if (!in_array($package, /** @scrutinizer ignore-type */ static::$packages) && file_exists(static::getPackagePath($package, false))) {
Loading history...
291
            static::$packages[] = $package;
292
293
            static::writeCacheFile(static::$package_file, static::$packages);
0 ignored issues
show
Bug introduced by
It seems like static::packages can also be of type boolean; however, parameter $data of LCI\MODX\Orchestrator\Or...rator::writeCacheFile() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

293
            static::writeCacheFile(static::$package_file, /** @scrutinizer ignore-type */ static::$packages);
Loading history...
294
        }
295
    }
296
297
    /**
298
     * @param string $file
299
     * @param array $data
300
     */
301
    protected static function writeCacheFile($file, $data)
302
    {
303
        $content = '<?php ' . PHP_EOL .
304
            'return ' . var_export($data, true) . ';';
305
306
        file_put_contents($file, $content);
307
    }
308
}
309