InstallOrchestrator::down()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
nc 24
nop 0
dl 0
loc 44
rs 8.9297
c 2
b 0
f 0
1
<?php
2
3
use \LCI\Blend\Migrations;
4
5
class InstallOrchestrator extends Migrations
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    /**
8
     * Run the migrations.
9
     *
10
     * @return void
11
     */
12
    public function up()
13
    {
14
        // Allow Custom .ENV
15
        $custom_base_path = getenv('LCI_ORCHESTRATOR_BASE_PATH');
16
        $custom_base_url = getenv('LCI_ORCHESTRATOR_BASE_URL');
17
        $custom_vendor_path = getenv('LCI_ORCHESTRATOR_VENDOR_PATH');
18
19
        // 1. MODX namespace
20
        $orchestratorNamespace = $this->modx->getObject('modNamespace', 'orchestrator');
21
22
        if (!$orchestratorNamespace) {
23
            /** @var \modNamespace $orchestratorNamespace */
24
            $orchestratorNamespace = $this->modx->newObject('modNamespace');
25
            $orchestratorNamespace->set('name', 'orchestrator');
26
            $orchestratorNamespace->set('path', '{core_path}vendor/');
27
            $orchestratorNamespace->set('assets_path', '{assets_path}orchestrator/');
28
29
            if ($orchestratorNamespace->save()) {
30
                $this->blender->outSuccess('The modNamespace orchestrator has been created');
31
            } else {
32
                $this->blender->out('The modNamespace orchestrator was not created', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $verbose of LCI\Blend\Blender::out(). ( Ignorable by Annotation )

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

32
                $this->blender->out('The modNamespace orchestrator was not created', /** @scrutinizer ignore-type */ true);
Loading history...
33
            }
34
        }
35
36
        // 2. Media source
37
        $orchestratorMediaSource = $this->blender->getBlendableLoader()->getBlendableMediaSource('orchestrator');
38
        $saved = $orchestratorMediaSource
39
            ->setFieldDescription('Orchestrator packages')
40
            ->setPropertyBasePath(!$custom_base_path ? 'core/vendor/' : $custom_base_path)
41
            ->setPropertyBaseUrl(!$custom_base_url ? 'core/vendor/' : $custom_base_url)
42
            ->blend();
43
44
        if ($saved) {
45
            $this->blender->outSuccess('orchestrator MediaSource has been installed');
46
        } else  {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; 2 found
Loading history...
47
            $this->blender->out('orchestrator MediaSource was not installed', true);
48
        }
49
50
        // 3. System setting
51
        /** @var \LCI\Blend\Blendable\SystemSetting $systemSetting */
52
        $systemSetting = $this->blender->getBlendableLoader()->getBlendableSystemSetting('orchestrator.vendor_path');
53
        $saved = $systemSetting
54
            ->setSeedsDir($this->getSeedsDir())
55
            ->setFieldValue(!$custom_vendor_path ? MODX_CORE_PATH.'vendor/' : $custom_vendor_path)
0 ignored issues
show
Bug introduced by
The constant MODX_CORE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
            ->setFieldArea('Composer')
57
            ->setFieldNamespace('orchestrator')
58
            ->blend();
59
60
        if ($saved) {
61
            $this->blender->outSuccess('orchestrator.vendor_path System Setting has been created');
62
        } else {
63
            $this->blender->outError('orchestrator.vendor_path System Setting was not created');
64
        }
65
66
        // 4. plugin
67
        $plugin = $this->blender->getBlendableLoader()->getBlendablePlugin('requireComposerAutoloader');
68
69
        $saved = $plugin
70
            ->setFieldCategory('Orchestrator')
71
            ->setFieldDescription('Will load the composer autoloader.php file inside of MODX')
72
            ->setAsStatic('lci/orchestrator/src/elements/plugins/requireComposerAutoloader.php', 'orchestrator')
73
            ->attachOnEvent('OnInitCulture')
74
            ->blend();
75
76
        if ($saved) {
77
            $this->blender->outSuccess('requireComposerAutoloader Plugin has been installed');
78
        } else {
79
            $this->blender->outError('requireComposerAutoloader Plugin has been installed');
80
        }
81
82
        $this->modx->cacheManager->refresh();
83
    }
84
85
    /**
86
     * Reverse the migrations.
87
     *
88
     * @return void
89
     */
90
    public function down()
91
    {
92
        // remove DB Table:
93
        $manager = $this->modx->getManager();
0 ignored issues
show
Unused Code introduced by
The assignment to $manager is dead and can be removed.
Loading history...
94
95
        // 4. plugin
96
        $plugin = $this->blender->getBlendableLoader()->getBlendablePlugin('requireComposerAutoloader');
97
98
        if ($plugin->revertBlend()) {
99
            $this->blender->outSuccess('requireComposerAutoloader Plugin has been reverted');
100
        } else {
101
            $this->blender->out('requireComposerAutoloader Plugin could not be reverted', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $verbose of LCI\Blend\Blender::out(). ( Ignorable by Annotation )

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

101
            $this->blender->out('requireComposerAutoloader Plugin could not be reverted', /** @scrutinizer ignore-type */ true);
Loading history...
102
        }
103
104
        // 3. System setting
105
        /** @var \LCI\Blend\Blendable\SystemSetting $systemSetting */
106
        $systemSetting = $this->blender->getBlendableLoader()->getBlendableSystemSetting('orchestrator.vendor_path');
107
        if ($systemSetting->revertBlend()) {
108
            $this->blender->outSuccess('orchestrator.vendor_path System Setting has been reverted');
109
        } else {
110
            $this->blender->out('orchestrator.vendor_path System Setting could not be reverted', true);
111
        }
112
113
        // 2. Media source
114
        $orchestratorMediaSource = $this->blender->getBlendableLoader()->getBlendableMediaSource('orchestrator');
115
        if ($orchestratorMediaSource->revertBlend()) {
116
            $this->blender->outSuccess('orchestrator MediaSource has been reverted');
117
        } else {
118
            $this->blender->out('orchestrator MediaSource could not be reverted', true);
119
        }
120
121
        // 1. MODX namespace
122
        /** @var \modNamespace $orchestratorNamespace */
123
        $orchestratorNamespace = $this->modx->getObject('modNamespace', 'orchestrator');
124
125
        if ($orchestratorNamespace instanceof \modNamespace) {
0 ignored issues
show
Bug introduced by
The type modNamespace 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...
126
            if ($orchestratorNamespace->remove()) {
127
                $this->blender->outSuccess('orchestrator modNamespace has been removed');
128
            } else {
129
                $this->blender->out('orchestrator modNamespace could not be removed', true);
130
            }
131
        }
132
133
        $this->modx->cacheManager->refresh();
134
    }
135
136
    /**
137
     * Method is called on construct, please fill me in
138
     */
139
    protected function assignDescription()
140
    {
141
        $this->description = 'Install Orchestrator';
142
    }
143
144
    /**
145
     * Method is called on construct, please fill me in
146
     */
147
    protected function assignVersion()
148
    {
149
        $this->version = $this->blender->getVersion();
150
    }
151
152
    /**
153
     * Method is called on construct, can change to only run this migration for those types
154
     */
155
    protected function assignType()
156
    {
157
        $this->type = 'master';
158
    }
159
160
    /**
161
     * Method is called on construct, Child class can override and implement this
162
     */
163
    protected function assignSeedsDir()
164
    {
165
        $this->seeds_dir = '2018_09_03_020202';
166
    }
167
}
168