App   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 165
Duplicated Lines 21.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 3
dl 36
loc 165
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 13 13 2
B update() 0 35 4
A uninstall() 13 13 2
B addApp() 0 29 5
B removeApp() 5 27 4
C updateApp() 5 39 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Installer\Actions;
2
3
use \Comodojo\Installer\Components\Filesystem;
4
use \Comodojo\Exception\InstallerException;
5
use \Exception;
6
7
/**
8
 * Comodojo Installer
9
 *
10
 * @package     Comodojo Framework
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     GPL-3.0+
13
 *
14
 * LICENSE:
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 */
29
30
class App extends AbstractAction {
31
32 View Code Duplication
    public function install($package_name, $package_extra) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
34
        $io = $this->getIO();
35
36
        $io->write("<info>>>> Installing apps from ".$package_name."</info>");
37
38
        foreach ($package_extra as $app => $configuration) {
39
40
            $this->addApp($io, $package_name, $app, $configuration);
41
42
        }
43
44
    }
45
46
    public function update($package_name, $initial_extra, $target_extra) {
47
48
        $io = $this->getIO();
49
50
        $io->write("<info>>>> Updating apps from ".$package_name."</info>");
51
52
        $old_apps = array_keys($initial_extra);
53
        
54
        $new_apps = array_keys($target_extra);
55
        
56
        $uninstall = array_diff($old_apps, $new_apps);
57
58
        $install = array_diff($new_apps, $old_apps);
59
60
        $update = array_intersect($old_apps, $new_apps);
61
        
62
        foreach ( $uninstall as $app ) {
63
            
64
            $this->removeApp($io, $package_name, $app, $initial_extra[$app]);
65
            
66
        }
67
        
68
        foreach ( $install as $app ) {
69
            
70
            $this->addApp($io, $package_name, $app, $target_extra[$app]);
71
            
72
        }
73
        
74
        foreach ( $update as $app ) {
75
            
76
            $this->updateApp($io, $package_name, $app, $initial_extra[$app], $target_extra[$app]);
77
            
78
        }
79
80
    }
81
82 View Code Duplication
    public function uninstall($package_name, $package_extra) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
84
        $io = $this->getIO();
85
86
        $io->write("<info>>>> Removing apps from ".$package_name."</info>");
87
88
        foreach ($package_extra as $app => $configuration) {
89
90
            $this->removeApp($io, $package_name, $app, $configuration);
91
92
        }
93
94
    }
95
96
    private function addApp($io, $package_name, $app, $configuration) {
97
        
98
        $description = empty($configuration['description']) ? null : $configuration['description'];
99
        
100
        $path = $this->getPath();
101
102
        $fs = new Filesystem();
103
        
104
        $assets = empty($configuration['assets']) ? null : $configuration['assets'];
105
106
        try {
107
            
108
            if ( $assets !== null ) {
109
            
110
                $fs->rcopy($path.'/'.$assets, COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_APP_ASSETS.'/'.$app);    
111
                
112
            }
113
114
            $this->getPackageInstaller()->apps()->add($package_name, $app, $description);
115
            
116
            $io->write(" <info>+</info> added app ".$app);
117
118
        } catch (Exception $e) {
119
120
            $io->write('<error>Error processing app: '.$e->getMessage().'</error>');
121
122
        }
123
        
124
    }
125
    
126
    private function removeApp($io, $package_name, $app, $configuration) {
0 ignored issues
show
Unused Code introduced by
The parameter $package_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
        
128
        $fs = new Filesystem();
129
        
130
        $assets = empty($configuration['assets']) ? null : $configuration['assets'];
131
        
132
        try {
133
134
            $id = $this->getPackageInstaller()->apps()->getByName($app)->getId();
135
136
            $this->getPackageInstaller()->apps()->delete($id);
137
            
138 View Code Duplication
            if ( $assets !== null ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
140
                $fs->rmdir(COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_APP_ASSETS.'/'.$app);
141
            
142
            }
143
144
            $io->write(" <comment>-</comment> removed app ".$app);
145
146
        } catch (Exception $e) {
147
148
            $io->write('<error>Error processing app: '.$e->getMessage().'</error>');
149
150
        }
151
        
152
    }
153
    
154
    private function updateApp($io, $package_name, $app, $old_configuration, $new_configuration) {
155
        
156
        $description = empty($new_configuration['description']) ? null : $new_configuration['description'];
157
        
158
        $path = $this->getPath();
159
160
        $fs = new Filesystem();
161
        
162
        $old_assets = empty($old_configuration['assets']) ? null : $old_configuration['assets'];
163
        
164
        $new_assets = empty($new_configuration['assets']) ? null : $new_configuration['assets'];
165
        
166
        try {
167
            
168
            $id = $this->getPackageInstaller()->apps()->getByName($app)->getId();
169
            
170
            $this->getPackageInstaller()->apps()->update($id, $package_name, $app, $description);
171
172 View Code Duplication
            if ( $old_assets !== null ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
                
174
                $fs->rmdir(COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_APP_ASSETS.'/'.$app);
175
                
176
            }
177
            
178
            if ( $new_assets !== null ) {
179
                
180
                $fs->rcopy($path.'/'.$new_assets, COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_APP_ASSETS.'/'.$app);
181
                
182
            }
183
184
            $io->write(" <comment>~</comment> updated app ".$app);
185
186
        } catch (Exception $e) {
187
188
            $io->write('<error>Error processing app: '.$e->getMessage().'</error>');
189
190
        }
191
        
192
    }
193
194
}
195