Service::validateService()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 3
eloc 2
nc 3
nop 1
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
class Service extends AbstractAction {
30
31
    public function install($package_name, $package_extra) {
32
33
        $io = $this->getIO();
34
35
        $io->write("<info>>>> Installing (dispatcher) services from package ".$package_name."</info>");
36
37
        $this->processService($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) services from package ".$package_name."</info>");
46
47
        $this->processService($io, 'uninstall', $package_name, $initial_extra);
48
49
        $this->processService($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) services from package ".$package_name."</info>");
58
59
        $this->processService($io, 'uninstall', $package_name, $package_extra);
60
61
    }
62
63
    private static function processService($io, $action, $package_name, $package_extra) {
64
65
        foreach ($package_extra as $service) {
66
67
            try {
68
69
                if ( !self::validateService($service) ) throw new InstallerException('Skipping invalid service in '.$package_name);
70
71
                $route = $service["route"];
72
                
73
                $type = $service["type"];
74
                
75
                $class = $service["class"];
76
                
77
                $parameters = empty($service["parameters"]) ? array() : $service["parameters"];
78
79
                switch ($action) {
80
81
                    case 'install':
82
83
                        $this->getPackageInstaller()->services()->add($package_name, $route, $type, $class, $parameters);
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 route ".$route." (".$type.")");
86
87
                        break;
88
89
                    case 'uninstall':
90
91
                        DispatcherConfiguration::removeRoute($package_name, $service);
92
93
                        $io->write(" <comment>-</comment> disabled route ".$route." (".$type.")");
94
95
                        break;
96
97
                }
98
99
            } catch (Exception $e) {
100
101
                $io->write('<error>Error processing service: '.$e->getMessage().'</error>');
102
103
            }
104
105
        }
106
107
    }
108
109
    private static function validateService($service) {
110
111
        return !( empty($service["route"]) || empty($service["type"]) || empty($service["class"]) );
112
113
    }
114
115
}
116