Orchestrator   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Importance

Changes 9
Bugs 1 Features 2
Metric Value
eloc 97
c 9
b 1
f 2
dl 0
loc 292
rs 9.36
wmc 38

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getPackagePath() 0 20 4
A uninstallComposerPackage() 0 7 1
B copyAssets() 0 28 7
A updateAllOrchestratorComposerPackages() 0 6 2
A removePackageConfig() 0 15 4
A writeCacheFile() 0 6 1
A loadOrchestratorPackageInfo() 0 16 4
A uninstall() 0 22 3
A install() 0 9 2
A runMigrations() 0 19 2
A updateComposerPackage() 0 7 1
A addDependantPackageToConfig() 0 3 1
A savePackageConfig() 0 7 3
A installComposerPackage() 0 7 1
A getConsole() 0 8 2
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::savePackageConfig('lci/orchestrator');
43
        self::runMigrations('lci/orchestrator', ['blend_modx_migration_dir' => $path]);
44
    }
45
46
    /**
47
     * @throws \LCI\Blend\Exception\MigratorException
48
     */
49
    public static function uninstall()
50
    {
51
        /** @var \LCI\MODX\Console\Console $console */
52
        $console = static::getConsole();
53
        // 1. install BLend
54
        $handler = new VoidUserInteractionHandler();
55
56
        $path = getenv('LCI_ORCHESTRATOR_MIGRATION_PATH');
57
        if (empty($path)) {
58
            $path = static::getPackagePath();
59
        }
60
61
        $blender = new Blender($console->loadMODX(), $handler, ['blend_modx_migration_dir' => $path]);
62
63
        if (!$blender->isBlendInstalledInModx()) {
64
            $blender->install();
65
        }
66
67
        // 2. run Migrations ~ install & update
68
        $blender->runMigration('down');
69
70
        $blender->install('down');
71
    }
72
73
    /**
74
     * @param string $package ~ ex: lci/stockpile
75
     */
76
    public static function addDependantPackageToConfig($package)
77
    {
78
        static::savePackageConfig($package);
79
    }
80
81
    /**
82
     * @param string $package ~ ex: lci/stockpile
83
     * @TODO Review ~ this is called on via a post composer script set up in extra
84
     *
85
     */
86
    public static function copyAssets($package)
87
    {
88
        // public will copy into the MODX public root path
89
        // assets will keep the same pathing
90
        $package_path = self::getPackagePath($package, false);
91
92
        $console = static::getConsole();
93
        $config = $console->getConfig();
94
95
        $self = new Orchestrator();
96
        $self->setMode(0755);
97
98
        if (file_exists($package_path . 'public')) {
99
            $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...
100
            if (isset($config['LCI_ORCHESTRATOR_PUBLIC_PATH']) && file_exists($config['LCI_ORCHESTRATOR_PUBLIC_PATH'])) {
101
                $destination = $config['LCI_ORCHESTRATOR_PUBLIC_PATH'];
102
            }
103
104
            $self->copyDirectory($package_path . 'public', $destination);
105
        }
106
107
        if (file_exists($package_path . 'assets')) {
108
            $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...
109
            if (isset($config['LCI_ORCHESTRATOR_ASSETS_PATH']) && file_exists($config['LCI_ORCHESTRATOR_ASSETS_PATH'])) {
110
                $destination = $config['LCI_ORCHESTRATOR_ASSETS_PATH'];
111
            }
112
113
            $self->copyDirectory($package_path . 'assets', $destination);
114
        }
115
    }
116
117
    /**
118
     * @param string $project ~ a valid composer project like lci/blend
119
     * @param string $type
120
     *
121
     * @throws \LCI\Blend\Exception\MigratorException
122
     */
123
    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...
124
    {
125
        $path = static ::getPackagePath($project);
126
127
        self::savePackageConfig($project);
128
        self::copyAssets($project);
129
        self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'up', $type);
130
    }
131
132
    /**
133
     * @param string $project ~ a valid composer project like lci/blend
134
     * @param string $type
135
     * @throws \LCI\Blend\Exception\MigratorException
136
     */
137
    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...
138
    {
139
        $path = static ::getPackagePath($project);
140
141
        self::savePackageConfig($project);
142
        self::copyAssets($project);
143
        self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'up', $type);
144
    }
145
146
    /**
147
     * @throws \LCI\Blend\Exception\MigratorException
148
     */
149
    public static function updateAllOrchestratorComposerPackages()
150
    {
151
        static::loadOrchestratorPackageInfo();
152
153
        foreach(static::$packages as $existing_package) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
154
            static::updateComposerPackage($existing_package);
155
        }
156
    }
157
158
    /**
159
     * @param string $project ~ a valid composer project like lci/blend
160
     * @param string $type
161
     * @throws \LCI\Blend\Exception\MigratorException
162
     */
163
    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...
164
    {
165
        $path = static ::getPackagePath($project);
166
167
        self::savePackageConfig($project);
168
        // @TODO remove assets
169
        self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'down', $type);
170
    }
171
172
    /**
173
     * @param string $project
174
     * @param array $config
175
     * @param string $method
176
     * @param string $type
177
     *
178
     * @throws \LCI\Blend\Exception\MigratorException
179
     */
180
    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...
181
    {
182
        /** @var \LCI\MODX\Console\Console $console */
183
        $console = static::getConsole();
184
        static::$modx = $console->loadMODX();
185
186
        // 1. install BLend
187
        $handler = new VoidUserInteractionHandler();
188
189
        $blender = new Blender(static::$modx, $handler, $config);
190
191
        if (!$blender->isBlendInstalledInModx()) {
192
            $blender->install();
193
        }
194
195
        $blender->setProject($project);
196
197
        // 2. run Migrations
198
        $blender->runMigration($method, $type);
199
    }
200
201
    /**
202
     * @param string $package
203
     * @param bool $include_src
204
     * @return string
205
     */
206
    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...
207
    {
208
        if (empty(static::$modx)) {
209
            /** @var \LCI\MODX\Console\Console $console */
210
            $console = static::getConsole();
211
            static::$modx = $console->loadMODX();
212
        }
213
214
        $path = static::$modx->getOption(
215
            'orchestrator.vendor_path',
216
            null,
217
            (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...
218
        );
219
        $path .= $package . DIRECTORY_SEPARATOR;
220
221
        if ($include_src) {
222
            $path .= 'src' . DIRECTORY_SEPARATOR;
223
        }
224
225
        return $path;
226
    }
227
228
    /**
229
     * @return \LCI\MODX\Console\Console
230
     */
231
    protected static function getConsole()
232
    {
233
        if (!static::$console) {
234
            /** @var \LCI\MODX\Console\Console $console */
235
            static::$console = new Console();
236
        }
237
238
        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...
239
    }
240
241
    /**
242
     *
243
     */
244
    protected static function loadOrchestratorPackageInfo()
245
    {
246
        /** @var \LCI\MODX\Console\Console $console */
247
        $console = static::getConsole();
248
249
        if (empty(static::$modx)) {
250
            static::$modx = $console->loadMODX();
251
        }
252
253
        if (!static::$packages) {
254
            static::$packages = [];
255
256
            static::$package_file = $console->getConfigFilePaths()['config_dir'] . 'lci_orchestrator_package.php';
257
258
            if (file_exists(static::$package_file)) {
259
                static::$packages = include static::$package_file;
260
            }
261
        }
262
    }
263
264
    /**
265
     * @param string $package
266
     */
267
    protected static function removePackageConfig($package)
268
    {
269
        static::loadOrchestratorPackageInfo();
270
        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

270
        if (in_array($package, /** @scrutinizer ignore-type */ static::$packages)) {
Loading history...
271
            $temp = [];
272
            foreach(static::$packages as $existing_package) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
273
                if ($existing_package == $package) {
274
                    continue;
275
                }
276
277
                $temp[] = $existing_package;
278
            }
279
            static::$packages = $temp;
280
281
            static::writeCacheFile(static::$package_file, static::$packages);
282
        }
283
    }
284
285
    /**
286
     * @param string $package
287
     */
288
    protected static function savePackageConfig($package)
289
    {
290
        static::loadOrchestratorPackageInfo();
291
        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

291
        if (!in_array($package, /** @scrutinizer ignore-type */ static::$packages) && file_exists(static::getPackagePath($package, false))) {
Loading history...
292
            static::$packages[] = $package;
293
294
            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

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