Code Duplication    Length = 89-89 lines in 2 locations

src/Actions/DispatcherPlugin.php 1 location

@@ 29-117 (lines=89) @@
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
class DispatcherPlugin extends AbstractAction {
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);
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

src/Actions/ExtenderPlugin.php 1 location

@@ 29-117 (lines=89) @@
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
class ExtenderPlugin extends AbstractAction {
30
31
    public function install($package_name, $package_extra) {
32
33
        $io = $this->getIO();
34
35
        $io->write("<info>>>> Installing (extender) 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 (extender) 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 (extender) 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.'-extender-'.$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, 'extender', $plugin_name, $event, $class, $method);
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