Completed
Push — master ( aa952b...cbe5fa )
by AJ
01:58
created

SubdomainsInstallShell::_uniqueSubdomains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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->_askInstall();
33
        } else {
34
            $continue = $this->_askUpdate();
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->_askAddSubdomain();              
46
                }
47
                
48
                if ($addMore) {
49
                        
50
                    if (!$this->_countSubdomains($subdomains)) {
51
                        $this->out();
52
                        $this->out('Add your first subdomain.');
53
                    }
54
                        
55
                    do {
56
                        
57
                        $this->out();
58
                        $subdomain = $this->_inputSubdomain();
59
                        $valid = $this->_validateSubdomain($subdomain);
60
                        $this->out();
61
                        
62
                        if ($valid) {
63
                            $subdomains[] = $subdomain;
64
                        } else {
65
                            $this->err('Invalid subdomain.');
66
                        }
67
                        
68
                    } while (!$valid || $this->_askAddSubdomain());
69
                    
70
                }
71
                
72
                $this->_displayCurrentUniqueSubdomains($subdomains);
73
                            
74
                while ($this->_countSubdomains($subdomains) && $this->_askDeleteSubdomain()) {
75
                    $deleteKey = $this->_inputDeleteSubdomain($subdomains);
76
                    $this->_deleteSubdomain($subdomains, $deleteKey);
77
                }
78
                
79
                $this->_writeConfig($subdomains);
80
                $this->_finalCheck($subdomains);
81
                
82
            } while (!$this->_countSubdomains($subdomains) && $this->_askStartOver());
83
            
84
            $this->_displayFinal($subdomains);
85
86
        }
87
    
88
    }
89
    
90
    private function _displayCurrentUniqueSubdomains(&$subdomains) {
91
        
92
        $subdomains = $this->_uniqueSubdomains($subdomains);
93
        $subdomains = $this->_modifyArray($subdomains);
94
        $this->_displayCurrentSubdomains($subdomains);
95
        
96
    }
97
        
98
    private function _uniqueSubdomains($subdomains) {
99
        
100
        return array_values(array_unique($subdomains));
101
        
102
    }
103
    
104
    private function _inputSubdomain() {
105
        
106
        return $this->in('Subdomain:');
107
        
108
    }
109
    
110
    private function _inputDeleteSubdomain($subdomains) {
111
        
112
        $this->out();
113
        return (int) $this->in('Enter number to delete:', array_keys($subdomains));    
114
            
115
    }
116
    
117
    private function _writeConfig($subdomains) {
118
        
119
        Configure::write('Multidimensional/Subdomains.subdomains', array_values($subdomains));
120
        Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);    
121
        
122
    }
123
    
124
    private function _displayFinal($subdomains) {
125
    
126
        $this->out();
127
        if ($this->_countSubdomains($subdomains)) {
128
            $this->out('Configuration saved!', 2);
129
        } else {
130
            $this->err('Plugin not currently active.', 2);    
131
        }
132
        
133
    }
134
    
135
    private function _finalCheck($subdomains) {
136
    
137
        if (!$this->_countSubdomains($subdomains)) {
138
            $this->out();
139
            $this->err('No subdomains configured.', 2);                    
140
        }    
141
        
142
    }
143
    
144
    private function _getSubdomains() {
145
    
146
        $check = Configure::check('Multidimensional/Subdomains.subdomains');
147
        
148
        if (!$check) {
149
            return false;    
150
        }
151
        
152
        $subdomains = Configure::consume('Multidimensional/Subdomains.subdomains');
153
        
154
        if ($this->_countSubdomains($subdomains)) {
155
            return $subdomains;
156
        }
157
        
158
        return false;
159
        
160
    }
161
    
162
    private function _modifyArray(array $array) {
163
164
        return array_combine(range(1, count($array)), array_values($array)); ;    
165
        
166
    }
167
    
168
    
169
    private function _displayCurrentSubdomains(array $subdomains) {
170
        
171
        if ($this->_countSubdomains($subdomains)) {
172
            
173
            $this->out();
174
            $this->out('Current subdomains:', 2);
175
                
176
            foreach ($subdomains AS $key => $value) {
177
                $this->out(' ' . ($key) . '. ' . $value);    
178
            }                
179
            
180
            $this->out();
181
        
182
        }
183
        
184
    }
185
    
186
    private function _askInstall() {
187
        
188
        return strtolower($this->in('Install subdomains plugin?', ['y', 'n'])) === 'y';
189
        
190
    }
191
    
192
    private function _askUpdate() {
193
            
194
        return strtolower($this->in('Update configuration?', ['y', 'n'])) === 'y';
195
            
196
    }
197
198
    private function _askAddSubdomain() {
199
    
200
        return strtolower($this->in('Add a subdomain?', ['y', 'n'])) === 'y';
201
        
202
    }
203
    
204
    private function _askDeleteSubdomain() {
205
    
206
        return strtolower($this->in('Delete a subdomain?', ['y', 'n'])) === 'y';
207
        
208
    }
209
    
210
    /**
211
     * @param integer $key
212
     */
213
    private function _deleteSubdomain(&$subdomains, $key) {
214
        
215
        if (isset($subdomains[$key])) {
216
            $this->out();
217
            $this->out('Deleted: ' . $subdomains[$key], 2);
218
            unset($subdomains[$key]);
219
            return true; 
220
        }
221
        
222
        return false;
223
        
224
    }
225
    
226
    /**
227
     * @param string|null $subdomain
228
     */    
229
    private function _validateSubdomain($subdomain) {
230
    
231
        if (is_null($subdomain) || empty($subdomain)) {
232
            return false;
233
        }
234
    
235
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
236
        
237
    }
238
    
239
    private function _askStartOver() {
240
        
241
        return strtolower($this->in('Start over?', ['y', 'n'])) === 'y';
242
        
243
    }
244
    
245
    private function _countSubdomains($subdomains) {
246
        
247
        if (!is_array($subdomains)) {
248
            return false;                                
249
        }
250
        
251
        if (is_null($subdomains)) {
252
            return false;    
253
        }
254
        
255
        if (count($subdomains) === 0) {
256
            return false;
257
        }
258
        
259
        return (int) count($subdomains);        
260
        
261
    }
262
      
263
}