Completed
Push — master ( 64c427...e3ca75 )
by AJ
01:53
created

SubdomainsInstallShell::_countSubdomains()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
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
class SubdomainsInstallShell extends Shell {
22
    
23
    public function main() {
24
        
25
        $this->clear();
26
        
27
        $this->helper('Multidimensional/Subdomains.Header')->output();
28
    
29
        $subdomains = $this->_getSubdomains();    
30
            
31
        if ($this->_countSubdomains($subdomains)) {
32
            $continue = $this->_inputYesNo('Update configuration?');
33
        } else {
34
            $continue = $this->_inputYesNo('Install subdomains plugin?');
35
        }
36
                  
37
        if ($continue) {
38
            
39
            do {
40
                
41
                $addMore = true;
42
                
43
                if ($this->_countSubdomains($subdomains)) {
44
                    $this->_displayCurrentUniqueSubdomains($subdomains);
45
                    $addMore = $this->_inputYesNo('Add a subdomain?');              
46
                }
47
                
48
                if ($addMore) {
49
                        
50
                    if (!$this->_countSubdomains($subdomains)) {
51
                            $this->out();
52
                        $this->out('Add your first subdomain.');
53
                    }
54
                        
55
                    $this->_inputSubdomain($subdomains);
56
                
57
                }
58
                            
59
                $this->_deleteSubdomain($subdomains);
60
                $this->_writeConfig($subdomains);
61
                $this->_finalCheck($subdomains);
62
                
63
            } while (!$this->_countSubdomains($subdomains) && $this->_inputYesNo('Start over?'));
64
            
65
            $this->_displayFinal($subdomains);
66
67
        }
68
    
69
    }
70
    
71
    private function _displayCurrentUniqueSubdomains(&$subdomains) {
72
        
73
        $subdomains = $this->_uniqueSubdomains($subdomains);
74
        $subdomains = $this->_modifyArray($subdomains);
75
        $this->_displayCurrentSubdomains($subdomains);
76
        
77
    }
78
    
79
    private function _inputSubdomain(&$subdomains) {
80
    
81
        do {
82
        
83
            $this->out();
84
            $subdomain = strtolower($this->in('Subdomain:'));
85
            $valid = $this->_validateSubdomain($subdomain);
86
            $this->out();
87
            
88
            if ($valid) {
89
                $subdomains[] = $subdomain;
90
            } else {
91
                $this->err('Invalid subdomain.');
92
            }
93
            
94
        } while (!$valid || $this->_inputYesNo('Add a subdomain?'));            
95
        
96
    }
97
        
98
    private function _uniqueSubdomains($subdomains) {
99
        
100
        return array_values(array_unique($subdomains));
101
        
102
    }
103
        
104
    private function _writeConfig($subdomains) {
105
        
106
        Configure::write('Multidimensional/Subdomains.subdomains', array_values($subdomains));
107
        Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);    
108
        
109
    }
110
    
111
    private function _displayFinal($subdomains) {
112
    
113
        $this->out();
114
        if ($this->_countSubdomains($subdomains)) {
115
            $this->out('Configuration saved!', 2);
116
        } else {
117
            $this->err('Plugin not currently active.', 2);    
118
        }
119
        
120
    }
121
    
122
    private function _finalCheck($subdomains) {
123
    
124
        if (!$this->_countSubdomains($subdomains)) {
125
            $this->out();
126
            $this->err('No subdomains configured.', 2);                    
127
        }    
128
        
129
    }
130
    
131
    private function _getSubdomains() {
132
    
133
        $check = Configure::check('Multidimensional/Subdomains.subdomains');
134
        
135
        if (!$check) {
136
            return false;    
137
        }
138
        
139
        $subdomains = Configure::consume('Multidimensional/Subdomains.subdomains');
140
        
141
        if ($this->_countSubdomains($subdomains)) {
142
            return $subdomains;
143
        }
144
        
145
        return false;
146
        
147
    }
148
    
149
    private function _modifyArray(array $array) {
150
151
        return array_combine(range(1, count($array)), array_values($array)); ;    
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
152
        
153
    }
154
    
155
    
156
    private function _displayCurrentSubdomains(array $subdomains) {
157
        
158
        if ($this->_countSubdomains($subdomains)) {
159
            
160
            $this->out();
161
            $this->out('Current subdomains:', 2);
162
                
163
            foreach ($subdomains AS $key => $value) {
164
                $this->out(' ' . ($key) . '. ' . $value);    
165
            }                
166
            
167
            $this->out();
168
        
169
        }
170
        
171
    }
172
    
173
    /**
174
    * @param string $string
175
    */     
176
    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...
177
    
178
        return strtolower($this->in($string, ['y', 'n'])) === 'y';    
179
        
180
    }
181
182
    private function _deleteSubdomain(&$subdomains) {
183
        
184
        $this->_displayCurrentUniqueSubdomains($subdomains);
185
        
186
        while ($this->_countSubdomains($subdomains) && $this->_inputYesNo('Delete a subdomain?')) {
187
188
            $this->out();
189
            $key = (int) $this->in('Enter number to delete:', array_keys($subdomains)); 
190
            
191
            if (isset($subdomains[$key])) {
192
                $this->out();
193
                $this->out('Deleted: ' . $subdomains[$key], 2);
194
                unset($subdomains[$key]);
195
            }
196
        
197
        }
198
                
199
    }
200
    
201
    /**
202
     * @param string|null $subdomain
203
     */    
204
    private function _validateSubdomain($subdomain) {
205
    
206
        if (is_null($subdomain) || empty($subdomain)) {
207
            return false;
208
        }
209
    
210
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
211
        
212
    }
213
    
214
    private function _countSubdomains($subdomains) {
215
       
216
        if (!isset($subdomains) || !is_array($subdomains)) {
217
            return 0;
218
        }
219
        
220
        return count($subdomains);
221
        
222
    }
223
      
224
}