DispatcherPlugin::processPlugin()   C
last analyzed

Complexity

Conditions 7
Paths 20

Size

Total Lines 47
Code Lines 20

Duplication

Lines 47
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 47
loc 47
rs 6.7272
cc 7
eloc 20
nc 20
nop 4
1
<?php namespace Comodojo\Installer\Actions;
2
3
use \Comodojo\Exception\InstallerException;
4
use \Exception;
5
6
/**
7
 * Comodojo Installer
8
 *
9
 * @package     Comodojo Framework
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29 View Code Duplication
class DispatcherPlugin extends AbstractAction {
0 ignored issues
show
Duplication introduced by
This class 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...
30
31
    public function install($package_name, $package_extra) {
32
33
        $io = $this->getIO();
34
35
        $io->write("<info>>>> Installing (dispatcher) plugins from package ".$package_name."</info>");
36
37
        $this->processPlugin($io, 'install', $package_name, $package_extra);
38
39
    }
40
41
    public function update($package_name, $initial_extra, $target_extra) {
42
43
        $io = $this->getIO();
44
45
        $io->write("<info>>>> Updating (dispatcher) plugins from package ".$package_name."</info>");
46
47
        $this->processPlugin($io, 'uninstall', $package_name, $initial_extra);
48
49
        $this->processPlugin($io, 'install', $package_name, $target_extra);
50
51
    }
52
53
    public function uninstall($package_name, $package_extra) {
54
55
        $io = $this->getIO();
56
57
        $io->write("<info>>>> Removing (dispatcher) plugins from package ".$package_name."</info>");
58
59
        $this->processPlugin($io, 'uninstall', $package_name, $package_extra);
60
61
    }
62
63
    private static function processPlugin($io, $action, $package_name, $package_extra) {
64
65
        foreach ($package_extra as $plugin => $configuration) {
66
67
            try {
68
69
                if ( !self::validatePlugin($configuration) ) throw new InstallerException('Skipping invalid plugin in '.$package_name);
70
71
                $plugin_name = $package_name.'-dispatcher-'.$plugin;
72
                
73
                $event = $configuration["event"];
74
                
75
                $class = $configuration["class"];
76
                
77
                $method = empty($configuration["method"]) ? null : $configuration["method"];
78
                
79
                switch ($action) {
80
81
                    case 'install':
82
83
                        $this->getPackageInstaller()->plugins()->add($package_name, 'dispatcher', $plugin_name, $event, $class, $method);
0 ignored issues
show
Bug introduced by
The variable $this 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...
84
85
                        $io->write(" <info>+</info> enabled plugin ".$plugin_name." on event ".$event);
86
87
                        break;
88
89
                    case 'uninstall':
90
91
                        $id = $this->getPackageInstaller()->plugins()->byName($plugin_name)->getId();
92
93
                        $this->getPackageInstaller()->plugins()->delete($id);
94
95
                        $io->write(" <comment>-</comment> disabled plugin ".$plugin_name." on event ".$event);
96
97
                        break;
98
99
                }
100
101
            } catch (Exception $e) {
102
103
                $io->write('<error>Error processing plugin: '.$e->getMessage().'</error>');
104
105
            }
106
107
        }
108
109
    }
110
111
    private static function validatePlugin($plugin) {
112
113
        return !( empty($plugin["class"]) || empty($plugin["event"]) );
114
115
    }
116
117
}
118