Test Setup Failed
Push — master ( 4706b6...910f0d )
by Josh
04:46
created

ActivePackageCommands::isBlendInstalled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: joshgulledge
5
 * Date: 9/1/18
6
 * Time: 10:54 PM
7
 */
8
9
namespace LCI\Blend\Console;
10
11
use LCI\Blend\Blender;
12
use LCI\MODX\Console\Application;
13
use LCI\MODX\Console\Command\PackageCommands;
14
use LCI\MODX\Console\Console;
15
use LCI\MODX\Console\Helpers\VoidUserInteractionHandler;
16
17
class ActivePackageCommands implements PackageCommands
18
{
19
    /** @var Console  */
20
    protected $console;
21
22
    /** @var array  */
23
    protected $commands = [
24
        'blend_installed' => [
25
            'LCI\Blend\Console\Migrate',
26
            'LCI\Blend\Console\Seed'
27
        ],
28
        'blend_not_installed' => [
29
30
        ],
31
        'modx_installed' => [
32
            'LCI\Blend\Console\Blend'
33
        ],
34
        'modx_not_installed' => [
35
            // @TODO command will be removed, use Gitify
36
            'LCI\Blend\Console\Modx\Install',
37
        ]
38
    ];
39
40
    public function __construct(Console $console)
41
    {
42
        $this->console = $console;
43
    }
44
45
    /**
46
     * @return array ~ of Fully qualified names of all command class
47
     */
48
    public function getAllCommands()
49
    {
50
        $all_commands = [];
51
        foreach ($this->commands as $group => $commands) {
52
            foreach ($commands as $command) {
53
                $all_commands[] = $command;
54
            }
55
        }
56
57
        return $all_commands;
58
    }
59
60
    /**
61
     * @return array ~ of Fully qualified names of active command classes. This could differ from all if package creator
62
     *      has different commands based on the state like the DB. Example has Install and Uninstall, only one would
63
     *      be active/available depending on the state
64
     */
65
    public function getActiveCommands()
66
    {
67
        $active_commands = [];
68
69
        if ($this->console->isModxInstalled()) {
70
71
            $commands = $this->commands['modx_installed'];
72
            foreach ($commands as $command) {
73
                $active_commands[] = $command;
74
            }
75
76
            if ($this->isBlendInstalled() && !$this->isBlendRequireUpdate()) {
77
                $commands = $this->commands['blend_installed'];
78
                foreach ($commands as $command) {
79
                    $active_commands[] = $command;
80
                }
81
            }
82
83
        } else {
84
            $commands = $this->commands['modx_not_installed'];
85
            foreach ($commands as $command) {
86
                $active_commands[] = $command;
87
            }
88
        }
89
90
        return $active_commands;
91
    }
92
93
    /**
94
     * @param \LCI\MODX\Console\Application $application
95
     * @return \LCI\MODX\Console\Application
96
     */
97
    public function loadActiveCommands(Application $application)
98
    {
99
        $commands = $this->getActiveCommands();
100
101
        foreach ($commands as $command) {
102
            $class = new $command();
103
104
            if (is_object($class) ) {
105
                if (method_exists($class, 'addConsole')) {
106
                    $class->addConsole($this->console);
107
                }
108
109
                $application->add($class);
110
            }
111
        }
112
113
        return $application;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    public function isBlendInstalled()
120
    {
121
        $blend = new Blender($this->console->loadMODX(), new VoidUserInteractionHandler(), []);
122
        return $blend->isBlendInstalledInModx();
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function isBlendRequireUpdate()
129
    {
130
        $blend = new Blender($this->console->loadMODX(), new VoidUserInteractionHandler(), []);
131
        return $blend->requireUpdate();
132
    }
133
}