Theme::processTheme()   C
last analyzed

Complexity

Conditions 7
Paths 27

Size

Total Lines 51
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 6.9743
cc 7
eloc 22
nc 27
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Theme 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 themes from package ".$package_name."</info>");
37
38
        $this->processTheme($io, 'install', $package_name, $package_extra);
39
40
    }
41
42
    public function update($package_name, $initial_extra, $target_extra) {
43
44
        $io = $this->getIO();
45
46
        $io->write("<info>>>> Updating themes from package ".$package_name."</info>");
47
48
        $this->processTheme($io, 'uninstall', $package_name, $package_extra);
0 ignored issues
show
Bug introduced by
The variable $package_extra 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...
49
50
        $this->processTheme($io, 'install', $package_name, $package_extra);
51
52
    }
53
54 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...
55
56
        $io = $this->getIO();
57
58
        $io->write("<info>>>> Removing themes from package ".$package_name."</info>");
59
60
        $this->processTheme($io, 'uninstall', $package_name, $package_extra);
61
62
    }
63
64
    private function processTheme($io, $action, $package_name, $package_extra) {
65
66
        foreach ($package_extra as $theme => $configuration) {
67
68
            try {
69
70
                if ( !self::validateTheme($configuration) ) throw new InstallerException('Skipping invalid theme in '.$package_name);
71
72
                $assets = $configuration['assets'];
73
                
74
                $description = empty($configuration['description']) ? null : $configuration['description'];
75
                
76
                $fs = new Filesystem();
77
                
78
                $path = $this->getPath();
79
80
                switch ($action) {
81
82
                    case 'install':
83
84
                        $fs->rcopy($path.'/'.$assets, COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_THEME_ASSETS.'/'.$theme);
85
                        
86
                        $this->getPackageInstaller()->themes()->add($package_name, $theme, $description);
87
88
                        $io->write(" <info>+</info> added theme ".$theme);
89
90
                        break;
91
92
                    case 'uninstall':
93
94
                        $id = $this->getPackageInstaller()->themes()->getByName($name)->getId();
0 ignored issues
show
Bug introduced by
The variable $name 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...
95
96
                        $this->getPackageInstaller()->themes()->delete($id);
97
                        
98
                        $fs->rmdir(COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_THEME_ASSETS.'/'.$theme);
99
100
                        $io->write(" <comment>-</comment> removed theme ".$theme);
101
102
                        break;
103
104
                }
105
106
            } catch (Exception $e) {
107
108
                $io->write('<error>Error processing theme: '.$e->getMessage().'</error>');
109
110
            }
111
112
        }
113
114
    }
115
116
    private static function validateTheme($theme) {
117
118
        return !( empty($theme['assets']) );
119
120
    }
121
122
}
123