Authentication   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 57.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 2
dl 80
loc 139
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 13 13 2
B update() 35 35 4
A uninstall() 13 13 2
A addAuthProvider() 0 21 4
A removeAuthProvider() 19 19 3
B updateAuthProvider() 0 23 4
A validateProvider() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Authentication 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 authentication providers from ".$package_name."</info>");
37
38
        foreach ($package_extra as $provider => $configuration) {
39
40
            $this->addAuthProvider($io, $package_name, $provider, $configuration);
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 authentication providers from ".$package_name."</info>");
51
52
        $old_providers = array_keys($initial_extra);
53
        
54
        $new_providers = array_keys($target_extra);
55
        
56
        $uninstall = array_diff($old_providers, $new_providers);
57
58
        $install = array_diff($new_providers, $old_providers);
59
60
        $update = array_intersect($old_providers, $new_providers);
61
        
62
        foreach ( $uninstall as $provider ) {
63
            
64
            $this->removeAuthProvider($io, $package_name, $provider, $initial_extra[$provider]);
65
            
66
        }
67
        
68
        foreach ( $install as $provider ) {
69
            
70
            $this->addAuthProvider($io, $package_name, $provider, $target_extra[$provider]);
71
            
72
        }
73
        
74
        foreach ( $update as $provider ) {
75
            
76
            $this->updateAuthProvider($io, $package_name, $provider, $target_extra[$provider]);
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 authentication providers from ".$package_name."</info>");
87
88
        foreach ($package_extra as $provider => $configuration) {
89
90
            $this->removeAuthProvider($io, $package_name, $provider, $configuration);
91
92
        }
93
94
    }
95
    
96
    private function addAuthProvider($io, $package_name, $provider, $configuration) {
97
        
98
        try {
99
100
            if ( !self::validateProvider($configuration) ) throw new InstallerException('Skipping invalid authentication provider '.$provider.' in '.$package_name);
101
102
            $class = $configuration['class'];
103
            
104
            $description = empty($configuration['description']) ? null : $configuration['description'];
105
            
106
            $this->getPackageInstaller()->authentication()->add($package_name, $provider, $class, $description);
107
108
            $io->write(" <info>+</info> added authentication provider ".$provider);
109
110
        } catch (Exception $e) {
111
112
            $io->write('<error>Error processing authentication provider: '.$e->getMessage().'</error>');
113
114
        }
115
        
116
    }
117
    
118 View Code Duplication
    private function removeAuthProvider($io, $package_name, $provider, $configuration) {
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...
119
        
120
        try {
121
122
            if ( !self::validateProvider($configuration) ) throw new InstallerException('Skipping invalid authentication provider '.$provider.' in '.$package_name);
123
            
124
            $id = $this->getPackageInstaller()->authentication()->getByName($provider)->getId();
125
126
            $this->getPackageInstaller()->authentication()->delete($id);
127
128
            $io->write(" <comment>-</comment> removed authentication provider ".$provider);
129
130
        } catch (Exception $e) {
131
132
            $io->write('<error>Error processing authentication provider: '.$e->getMessage().'</error>');
133
134
        }
135
        
136
    }
137
    
138
    private function updateAuthProvider($io, $package_name, $provider, $configuration) {
139
        
140
        try {
141
142
            if ( !self::validateProvider($configuration) ) throw new InstallerException('Skipping invalid authentication provider '.$provider.' in '.$package_name);
143
144
            $class = $configuration['class'];
145
            
146
            $description = empty($configuration['description']) ? null : $configuration['description'];
147
148
            $id = $this->getPackageInstaller()->authentication()->getByName($provider)->getId();
149
            
150
            $this->getPackageInstaller()->authentication()->update($id, $package_name, $provider, $class, $description);
151
152
            $io->write(" <comment>~</comment> updated authentication provider ".$provider);
153
154
        } catch (Exception $e) {
155
156
            $io->write('<error>Error processing authentication provider: '.$e->getMessage().'</error>');
157
158
        }
159
        
160
    }
161
    
162
    private static function validateProvider($auth) {
163
164
        return !( empty($auth["class"]) );
165
166
    }
167
168
}
169