Setting::uninstall()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php namespace Comodojo\Installer\Actions;
2
3
use \Comodojo\Exception\InstallerException;
4
use \Exception;
5
6
/**
7
 *
8
 *
9
 * @package     Comodojo Framework
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @author      Marco Castiello <[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 Setting 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 setting from package ".$package_name."</info>");
37
38
        foreach ($package_extra as $setting => $value) {
39
40
            $this->addSetting($io, $package_name, $setting, $value);
41
42
        }
43
44
    }
45
46 View Code Duplication
    public function update($package_name, $initial_extra, $target_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...
47
48
        $io = $this->getIO();
49
50
        $io->write("<info>>>> Updating setting from package ".$package_name."</info>");
51
52
        $old_settings = array_keys($initial_extra);
53
        
54
        $new_settings = array_keys($target_extra);
55
        
56
        $uninstall = array_diff($old_settings, $new_settings);
57
58
        $install = array_diff($new_settings, $old_settings);
59
60
        $update = array_intersect($old_settings, $new_settings);
61
        
62
        foreach ( $uninstall as $setting ) {
63
            
64
            $this->removeSetting($io, $package_name, $setting, $initial_extra[$setting]);
65
            
66
        }
67
        
68
        foreach ( $install as $setting ) {
69
            
70
            $this->addSetting($io, $package_name, $setting, $target_extra[$setting]);
71
            
72
        }
73
        
74
        foreach ( $update as $setting ) {
75
            
76
            $this->updateSetting($io, $package_name, $setting, $initial_extra[$setting], $target_extra[$setting]);
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 setting from package ".$package_name."</info>");
87
88
        foreach ($package_extra as $setting => $value) {
89
90
            $this->addSetting($io, $package_name, $setting, $value);
91
92
        }
93
94
    }
95
96
    private function addSetting($io, $package_name, $setting, $value) {
97
        
98
        try {
99
100
            if ( !self::validateSetting($value) ) throw new InstallerException('Skipping invalid setting '.$setting.' in '.$package_name);
101
102
            $this->getPackageInstaller()->settings()->add($package_name, $setting, $value);
103
104
            $io->write(" <info>+</info> added setting ".$setting);
105
106
        } catch (Exception $e) {
107
108
            $io->write('<error>Error processing setting: '.$e->getMessage().'</error>');
109
110
        }
111
        
112
    }
113
    
114 View Code Duplication
    private function removeSetting($io, $package_name, $setting, $value) {
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...
115
        
116
        try {
117
118
            if ( !self::validateSetting($value) ) throw new InstallerException('Skipping invalid setting '.$setting.' in '.$package_name);
119
120
            $id = $this->getPackageInstaller()->settings()->getByName($setting)->getId();
121
122
            $this->getPackageInstaller()->settings()->delete($id);
123
124
            $io->write(" <comment>-</comment> removed setting ".$setting);
125
126
        } catch (Exception $e) {
127
128
            $io->write('<error>Error processing setting: '.$e->getMessage().'</error>');
129
130
        }
131
        
132
    }
133
    
134
    private function updateSetting($io, $package_name, $setting, $old_value, $new_value) {
135
        
136
        try {
137
138
            if ( !self::validateSetting($value) ) throw new InstallerException('Skipping invalid setting '.$setting.' in '.$package_name);
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
139
140
            $old = $this->getPackageInstaller()->settings()->getByName($setting);
141
            
142
            if ( $old->getValue() == $old_value ) {
143
                
144
                $this->getPackageInstaller()->settings()->update($old->getId(), $package_name, $setting, $new_value);
145
                
146
                $io->write(" <comment>~</comment> updated setting ".$setting);
147
                
148
            } else if ( $io->askConfirmation("Replace modified setting ".$setting."?", false) === true ) {
149
                
150
                $this->getPackageInstaller()->settings()->update($old->getId(), $package_name, $setting, $new_value);
151
                
152
                $io->write(" <comment>~</comment> updated setting ".$setting);
153
                
154
            } else {
155
                
156
                $io->write(" <comment>x</comment> modified setting ".$setting." not replaced");
157
                
158
            }
159
160
        } catch (Exception $e) {
161
162
            $io->write('<error>Error processing setting: '.$e->getMessage().'</error>');
163
164
        }
165
        
166
    }
167
168
    private static function validateSetting($value) {
169
170
        return is_scalar($value);
171
172
    }
173
174
}
175