ActivePackageCommands   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 41
c 3
b 1
f 0
dl 0
loc 109
rs 10
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllCommands() 0 10 3
A isOrchestratorInstalled() 0 8 2
A isOrchestratorRequireUpdate() 0 3 1
A getActiveCommands() 0 21 6
A __construct() 0 3 1
A loadActiveCommands() 0 17 4
1
<?php
2
3
namespace LCI\MODX\Orchestrator\Console;
4
5
use LCI\MODX\Console\Application;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, LCI\MODX\Orchestrator\Console\Application. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use LCI\MODX\Console\Command\PackageCommands;
7
use LCI\MODX\Console\Console;
8
9
class ActivePackageCommands implements PackageCommands
10
{
11
    /** @var Console  */
12
    protected $console;
13
14
    /** @var array  */
15
    protected $commands = [
16
        'orchestrator_installed' => [
17
            'LCI\MODX\Orchestrator\Console\Command\DeployCommand',
18
            'LCI\MODX\Orchestrator\Console\Command\InstallPackages',
19
            'LCI\MODX\Orchestrator\Console\Command\UninstallPackages',
20
            'LCI\MODX\Orchestrator\Console\Command\ListMODXPackages',
21
            'LCI\MODX\Orchestrator\Console\Command\RequireMODXPackages',
22
        ],
23
        'orchestrator_not_installed' => [
24
            'LCI\MODX\Orchestrator\Console\Command\Package'
25
        ]
26
    ];
27
28
    public function __construct(Console $console)
29
    {
30
        $this->console = $console;
31
    }
32
33
    /**
34
     * @return array ~ of Fully qualified names of all command class
35
     */
36
    public function getAllCommands()
37
    {
38
        $all_commands = [];
39
        foreach ($this->commands as $group => $commands) {
40
            foreach ($commands as $command) {
41
                $all_commands[] = $command;
42
            }
43
        }
44
45
        return $all_commands;
46
    }
47
48
    /**
49
     * @return array ~ of Fully qualified names of active command classes. This could differ from all if package creator
50
     *      has different commands based on the state like the DB. Example has Install and Uninstall, only one would
51
     *      be active/available depending on the state
52
     */
53
    public function getActiveCommands()
54
    {
55
        $active_commands = [];
56
57
        if ($this->console->isModxInstalled()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
58
59
            if ($this->isOrchestratorInstalled() && !$this->isOrchestratorRequireUpdate()) {
60
                $commands = $this->commands['orchestrator_installed'];
61
                foreach ($commands as $command) {
62
                    $active_commands[] = $command;
63
                }
64
            } else {
65
                $commands = $this->commands['orchestrator_not_installed'];
66
                foreach ($commands as $command) {
67
                    $active_commands[] = $command;
68
                }
69
            }
70
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
71
        }
72
73
        return $active_commands;
74
    }
75
76
    /**
77
     * @param \LCI\MODX\Console\Application $application
78
     * @return \LCI\MODX\Console\Application
79
     */
80
    public function loadActiveCommands(Application $application)
81
    {
82
        $commands = $this->getActiveCommands();
83
84
        foreach ($commands as $command) {
85
            $class = new $command();
86
87
            if (is_object($class) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
88
                if (method_exists($class, 'setConsole')) {
89
                    $class->setConsole($this->console);
90
                }
91
92
                $application->add($class);
93
            }
94
        }
95
96
        return $application;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function isOrchestratorInstalled()
103
    {
104
        $modx = $this->console->loadMODX();
105
        if (!empty($modx->getOption('orchestrator.vendor_path'))) {
106
            return true;
107
        }
108
109
        return false;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function isOrchestratorRequireUpdate()
116
    {
117
        return false;
118
    }
119
}
120