Completed
Push — 3.0 ( fea5eb...c29f0e )
by Vermeulen
02:17
created

ModuleInstall   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 4
dl 0
loc 130
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A getListToInstall() 0 4 1
A init() 0 4 1
A toRun() 0 4 1
A run() 0 5 1
A addToList() 0 8 1
A installAllModules() 0 23 5
A installModule() 0 24 5
1
<?php
2
3
namespace BFW\Install\Core\AppSystems;
4
5
use \BFW\Core\AppSystems\AbstractSystem;
6
use \BFW\Helpers\Cli;
7
8
class ModuleInstall extends AbstractSystem
9
{
10
    /**
11
     * @var \BFW\Install\ModuleInstall[] $listToInstall
12
     */
13
    protected $listToInstall = [];
14
    
15
    /**
16
     * {@inheritdoc}
17
     * 
18
     * @return $this
19
     */
20
    public function __invoke()
21
    {
22
        return $this;
23
    }
24
    
25
    /**
26
     * Getter accessor to property listToInstall
27
     * 
28
     * @return type
29
     */
30
    public function getListToInstall()
31
    {
32
        return $this->listToInstall;
33
    }
34
    
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function init()
39
    {
40
        $this->initStatus = true;
41
    }
42
    
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function toRun()
47
    {
48
        return true;
49
    }
50
    
51
    /**
52
     * {@inheritdoc}
53
     * Run install of all modules
54
     */
55
    public function run()
56
    {
57
        $this->installAllModules();
58
        $this->runStatus = true;
59
    }
60
    
61
    /**
62
     * Add a new module to the list to install
63
     * 
64
     * @param \BFW\Install\ModuleInstall $module The new module
65
     * 
66
     * @return $this
67
     */
68
    public function addToList(\BFW\Install\ModuleInstall $module)
69
    {
70
        $moduleName = $module->getName();
71
        
72
        $this->listToInstall[$moduleName] = $module;
73
        
74
        return $this;
75
    }
76
    
77
    /**
78
     * Install all modules in the order of the dependency tree.
79
     * 
80
     * @return void
81
     */
82
    protected function installAllModules()
83
    {
84
        Cli::displayMsgNL('Read all modules to run install script...');
85
        
86
        $tree = \BFW\Install\Application::getInstance()
0 ignored issues
show
Documentation Bug introduced by
The method getModuleList does not exist on object<BFW\Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
87
            ->getModuleList()
88
            ->getLoadTree()
89
        ;
90
91
        foreach ($tree as $firstLine) {
92
            foreach ($firstLine as $secondLine) {
93
                foreach ($secondLine as $moduleName) {
94
                    if (!isset($this->listToInstall[$moduleName])) {
95
                        continue;
96
                    }
97
                    
98
                    $this->installModule($moduleName);
99
                }
100
            }
101
        }
102
        
103
        Cli::displayMsgNL('All modules have been read.');
104
    }
105
    
106
    /**
107
     * Install a module
108
     * 
109
     * @param string $moduleName The module name
110
     * 
111
     * @return void
112
     */
113
    protected function installModule($moduleName)
114
    {
115
        if (!isset($this->listToInstall[$moduleName])) {
116
            return;
117
        }
118
        
119
        Cli::displayMsgNL(' > Read for module '.$moduleName);
120
        
121
        $module         = $this->listToInstall[$moduleName];
122
        $installScripts = $module->getSourceInstallScript();
123
        
124
        if ($installScripts === '') {
125
            Cli::displayMsgNL(' >> No script to run.');
126
            return;
127
        }
128
        
129
        if (is_string($installScripts)) {
130
            $installScripts = (array) $installScripts;
131
        }
132
        
133
        foreach ($installScripts as $scriptPath) {
0 ignored issues
show
Bug introduced by
The expression $installScripts of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
134
            $module->runInstallScript($scriptPath);
135
        }
136
    }
137
}
138