Completed
Push — master ( f4e46e...01741b )
by AJ
01:55
created

SubdomainsInstallShell::_currentSubdomains()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
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) ? $this->_askInstall() : $this->_askUpdate()) {
32
            
33
            do {
34
                
35
                $addMore = true;
36
                
37
                if ($this->_countSubdomains($subdomains)) {
38
                
39
                    $subdomains = array_values(array_unique($subdomains));
40
                    $subdomains = $this->_modifyArray($subdomains);
41
                    $this->_currentSubdomains($subdomains);
42
                    $addMore = $this->_askAddSubdomain();
43
                                    
44
                }
45
                
46
                if ($addMore) {
47
                        
48
                    if (!$this->_countSubdomains($subdomains)) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $this->_countSubdomains($subdomains) of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
49
                        $this->out();
50
                        $this->out('Add your first subdomain.');
51
                    }
52
                        
53
                    do {
54
                        
55
                        $this->out();
56
                        
57
                        $subdomain = $this->in('Subdomain:');
58
                        $valid = $this->_validateSubdomain($subdomain);
59
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
                $subdomains = array_values(array_unique($subdomains));
73
                
74
                $subdomains = $this->_modifyArray($subdomains);
75
                
76
                $this->_currentSubdomains($subdomains);
77
                            
78
                while ($this->_countSubdomains($subdomains) && $this->_askDeleteSubdomain()) {
79
                    
80
                    $this->out();
81
                    $deleteKey = (int) $this->in('Enter number to delete:', array_keys($subdomains));
82
                    $this->_deleteSubdomain($subdomains, $deleteKey);
83
                    
84
                }
85
                
86
                Configure::write('Multidimensional/Subdomains.subdomains', array_values($subdomains));
87
                Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);
88
                
89
                if (!$this->_countSubdomains($subdomains)) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $this->_countSubdomains($subdomains) of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
90
                    $this->out();
91
                    $this->err('No subdomains configured.', 2);                    
92
                }
93
                
94
            } while (!$this->_countSubdomains($subdomains) && $this->_askStartOver());
1 ignored issue
show
Bug Best Practice introduced by
The expression $this->_countSubdomains($subdomains) of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
95
            
96
            $this->out();
97
            if ($this->_countSubdomains($subdomains)) {
98
                $this->out('Configuration saved!', 2);
99
            } else {
100
                $this->err('Plugin not currently active.', 2);    
101
            }
102
        }
103
    
104
    }
105
    
106
	private function _getSubdomains() {
107
	
108
	    $check = Configure::check('Multidimensional/Subdomains.subdomains');
109
		
110
		if (!$check) {
111
			return false;	
112
		}
113
		
114
		$subdomains = Configure::consume('Multidimensional/Subdomains.subdomains');
115
		
116
		if ($this->_countSubdomains($subdomains)) { 
117
			return $subdomains;
118
		}
119
		
120
        return false;
121
		
122
	}
123
	
124
    private function _modifyArray(array $array) {
125
126
        return array_combine(range(1, count($array)), array_values($array)); ;    
127
        
128
    }
129
    
130
    
131
    private function _currentSubdomains(array $subdomains) {
132
        
133
        if ($this->_countSubdomains($subdomains)) {
134
            
135
            $this->out();
136
            $this->out('Current subdomains:', 2);
137
                
138
            foreach ($subdomains AS $key => $value) {
139
                $this->out(' ' . ($key) . '. ' . $value);    
140
            }                
141
            
142
            $this->out();
143
        
144
        }
145
        
146
    }
147
    
148
    private function _askInstall() {
149
        
150
        return (strtolower($this->in('Install subdomains plugin?', ['y', 'n'])) === 'y');
151
        
152
    }
153
    
154
    private function _askUpdate() {
155
            
156
        return (strtolower($this->in('Update configuration?', ['y', 'n'])) === 'y');
157
            
158
    }
159
160
    private function _askAddSubdomain() {
161
    
162
        return (strtolower($this->in('Add a subdomain?', ['y', 'n'])) === 'y');
163
        
164
    }
165
    
166
    private function _askDeleteSubdomain() {
167
    
168
        return (strtolower($this->in('Delete a subdomain?', ['y', 'n'])) === 'y');
169
        
170
    }
171
    
172
    /**
173
     * @param integer $key
174
     */
175
    private function _deleteSubdomain(&$subdomains, $key) {
176
        
177
        if (isset($subdomains[$key])) {
178
            
179
            $this->out();
180
            $this->out('Deleted: ' . $subdomains[$key], 2);
181
            unset($subdomains[$key]);
182
        
183
            return true; 
184
        
185
        }
186
        
187
        return false;
188
        
189
    }
190
    
191
    /**
192
     * @param string|null $subdomain
193
     */    
194
    private function _validateSubdomain($subdomain) {
195
    
196
        if (is_null($subdomain) || empty($subdomain)) {
197
            return false;
198
        }
199
    
200
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
201
        
202
    }
203
    
204
    private function _askStartOver() {
205
        
206
        return (strtolower($this->in('Start over?', ['y', 'n'])) === 'y');
207
        
208
    }
209
    
210
    private function _countSubdomains($subdomains) {
211
        
212
        if (!is_array($subdomains)) {
213
            return false;                                
214
        }
215
		
216
        if (is_null($subdomains)) {
217
            return false;	
218
        }
219
		
220
        if (count($subdomains) === 0) {
221
            return false;
222
        }
223
        
224
        return (int) count($subdomains);        
225
        
226
    }
227
      
228
}