Completed
Push — 3.0 ( 2a6107...213cde )
by Vermeulen
01:45
created

ModuleInstall   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 6
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
B installModule() 0 24 6
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 \BFW\Install\ModuleInstall[]
29
     */
30
    public function getListToInstall(): array
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(): bool
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): self
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()
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(string $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 (empty($installScripts) || $installScripts === false) {
125
            Cli::displayMsgNL(' >> No script to run.');
126
            return;
127
        }
128
        
129
        if (!is_array($installScripts)) {
130
            $installScripts = [$installScripts];
131
        }
132
        
133
        foreach ($installScripts as $scriptPath) {
134
            $module->runInstallScript($scriptPath);
135
        }
136
    }
137
}
138