Completed
Branch Testing (40b0cd)
by AJ
02:19
created

SubdomainsInstallShell   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 36
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 224
ccs 0
cts 119
cp 0
rs 8.8

15 Methods

Rating   Name   Duplication   Size   Complexity  
B main() 0 26 4
A _runProgram() 0 9 2
A _displayCurrentUniqueSubdomains() 0 11 2
A _inputSubdomain() 0 21 4
A _uniqueSubdomains() 0 9 2
A _writeConfig() 0 6 1
A _displayFinal() 0 10 2
A _finalCheck() 0 8 2
A _getSubdomains() 0 6 1
A _modifyArray() 0 13 2
A _displayCurrentSubdomains() 0 16 3
A _inputYesNo() 0 5 1
A _deleteSubdomain() 0 16 4
A _validateSubdomain() 0 9 3
A _countSubdomains() 0 9 3
1
<?php
2
/**
3
 * CakePHP Plugin : CakePHP Subdomain Routing
4
 * Copyright (c) Multidimension.al (http://multidimension.al)
5
 * Github : https://github.com/multidimension-al/cakephp-subdomains
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE file
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     (c) Multidimension.al (http://multidimension.al)
12
 * @link          https://github.com/multidimension-al/cakephp-subdomains Github
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
namespace Multidimensional\Subdomains\Shell;
17
18
use Cake\Core\Configure;
19
use Cake\Console\Shell;
20
21
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware;
22
23
class SubdomainsInstallShell extends Shell {
24
    
25
    public function main() {
26
        
27
        $this->clear();
28
        
29
        $this->helper('Multidimensional/Subdomains.Header')->output();
30
    
31
        $subdomains = $this->_getSubdomains();    
32
        $continue = $this->_runProgram($subdomains);
33
                  
34
        if ($continue) {
35
            
36
            do {
37
                
38
                $this->_inputSubdomain($subdomains);  
39
                $this->_displayCurrentUniqueSubdomains($subdomains);          
40
                $this->_deleteSubdomain($subdomains);
41
                $this->_writeConfig($subdomains);
42
                $this->_finalCheck($subdomains);
43
                
44
            } while (!$this->_countSubdomains($subdomains) && $this->_inputYesNo('Start over?'));
45
46
        }
47
        
48
        $this->_displayFinal($subdomains);
49
    
50
    }
51
    
52
    private function _runProgram($subdomains) {
0 ignored issues
show
Coding Style introduced by
function _runProgram() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
53
                
54
        if ($this->_countSubdomains($subdomains)) {
55
            return $this->_inputYesNo('Update configuration?');    
56
        } else {
57
            return $this->_inputYesNo('Install subdomains plugin?');    
58
        }
59
        
60
    }
61
    
62
    /**
63
     * @return void
64
     */
65
    private function _displayCurrentUniqueSubdomains(&$subdomains) {
66
     
67
        if ($this->_countSubdomains($subdomains)) {
68
		
69
            $subdomains = $this->_uniqueSubdomains($subdomains);
70
            $subdomains = $this->_modifyArray($subdomains);
71
            $this->_displayCurrentSubdomains($subdomains);
72
		
73
        }
74
75
    }
76
    
77
    /**
78
     * @return void
79
     */
80
    private function _inputSubdomain(&$subdomains) {
81
    
82
        $valid = true;
83
        $this->out();
84
    
85
        while (!$valid || $this->_inputYesNo('Add a subdomain?')) {
86
        
87
            $this->out();
88
            $subdomain = strtolower($this->in('Subdomain:'));
89
            $valid = $this->_validateSubdomain($subdomain);
90
            $this->out();
91
            
92
            if ($valid) {
93
                $subdomains[] = $subdomain;
94
            } else {
95
                $this->err('Invalid subdomain.');
96
            }
97
            
98
        };            
99
        
100
    }
101
    
102
    /**
103
     * @return array $subdomains
104
     */   
105
    private function _uniqueSubdomains($subdomains) {
106
        
107
        if ($this->_countSubdomains($subdomains)) {
108
            return array_values(array_unique($subdomains));
109
        } else {
110
            return $subdomains;
111
        }
112
       
113
    }
114
     
115
    /**
116
     * @return void
117
     */
118
    private function _writeConfig($subdomains) {
119
        
120
        Configure::write('Multidimensional/Subdomains.Subdomains', array_values($subdomains));
121
        Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);    
122
        
123
    }
124
    
125
    /**
126
     * @return void
127
     */
128
    private function _displayFinal($subdomains) {
129
    
130
        $this->out();
131
        if ($this->_countSubdomains($subdomains)) {
132
            $this->out('Configuration saved!', 2);
133
        } else {
134
            $this->err('Plugin not currently active.', 2);    
135
        }
136
        
137
    }
138
    
139
    /**
140
     * @return void
141
     */
142
    private function _finalCheck($subdomains) {
143
    
144
        if (!$this->_countSubdomains($subdomains)) {
145
            $this->out();
146
            $this->err('No subdomains configured.', 2);                    
147
        }    
148
        
149
    }
150
    
151
    private function _getSubdomains() {
152
        
153
        $subdomainObject = new SubdomainMiddleware();
154
        return $subdomainObject->getSubdomains();
155
        
156
    }
157
    
158
    private function _modifyArray(array $subdomains) {
159
        
160
        if ($this->_countSubdomains($subdomains)) {
161
        
162
            return array_combine(
163
                range(1, count($subdomains)),
164
                array_values($subdomains));
165
                
166
        } else {
167
            return $subdomains;    
168
        }
169
        
170
    }
171
    
172
    /**
173
     * @return void
174
     */
175
    private function _displayCurrentSubdomains(array $subdomains) {
176
        
177
        if ($this->_countSubdomains($subdomains)) {
178
            
179
            $this->out();
180
            $this->out('Current subdomains:', 2);
181
                
182
            foreach ($subdomains AS $key => $value) {
183
                $this->out(' ' . ($key) . '. ' . $value);    
184
            }                
185
            
186
            $this->out();
187
        
188
        }
189
        
190
    }
191
    
192
    /**
193
     * @param string $string
194
     * @return bool
195
     */     
196
    private function _inputYesNo($string) {
0 ignored issues
show
Coding Style introduced by
function _inputYesNo() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
197
    
198
        return strtolower($this->in($string, ['y', 'n'])) === 'y';    
199
        
200
    }
201
202
    private function _deleteSubdomain(&$subdomains) {
203
        
204
        while ($this->_countSubdomains($subdomains) && $this->_inputYesNo('Delete a subdomain?')) {
205
206
            $this->out();
207
            $key = (int) $this->in('Enter number to delete:', array_keys($subdomains)); 
208
            
209
            if (isset($subdomains[$key])) {
210
                $this->out();
211
                $this->out('Deleted: ' . $subdomains[$key], 2);
212
                unset($subdomains[$key]);
213
            }
214
        
215
        }
216
                
217
    }
218
    
219
    /**
220
     * @param string|null $subdomain
221
     * @return string|null
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
222
     */    
223
    private function _validateSubdomain($subdomain) {
224
    
225
        if (is_null($subdomain) || empty($subdomain)) {
226
            return false;
227
        }
228
    
229
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
230
        
231
    }
232
    
233
    /**
234
     * @return int
235
     */
236
    private function _countSubdomains($subdomains) {
237
       
238
        if (!isset($subdomains) || !is_array($subdomains)) {
239
            return 0;
240
        }
241
        
242
        return count($subdomains);
243
        
244
    }
245
      
246
}
247