Completed
Push — master ( 3a05b6...c459c5 )
by AJ
01:54
created

SubdomainsInstallShell::_askDeleteSubdomain()   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 0
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
        $subdomains = array();    
26
    
27
        $this->clear();
28
        
29
        $this->helper('Multidimensional/Subdomains.Header')->output();
1 ignored issue
show
Bug introduced by
The call to output() misses a required argument $args.

This check looks for function calls that miss required arguments.

Loading history...
30
    
31
        $first_run = Configure::check('Multidimensional/Subdomains.subdomains') ? false : true;
32
        
33
        if ($first_run === false) {
34
            
35
            $subdomains = Configure::consume('Multidimensional/Subdomains.subdomains');
36
            
37
            if (count($subdomains) == 0) {
38
                $first_run = true;
39
            }
40
            
41
        }
42
       
43
        if ((($first_run) ? (strtolower($this->in('Install Subdomains Plugin?', ['y', 'n'])) == 'y') : (strtolower($this->in('Update Configuration?', ['y', 'n'])) == 'y'))) {
44
            
45
            do {
46
                
47
                $addMore = true;
48
                
49
                if ($first_run === false && $this->_countSubdomains($subdomains)) {
50
                
51
					$subdomains = array_values(array_unique($subdomains));
52
					
53
					$subdomains = $this->_modifyArray($subdomains);
54
					
55
					$this->_currentSubdomains($subdomains);
56
                    
57
                    $addMore = $this->_askAddSubdomain();
58
                                    
59
                }
60
                
61
                if ($addMore) {
62
                        
63
                    if (!$this->_countSubdomains($subdomains)) {
64
						$this->out();
65
                        $this->out('Add Your First Subdomain.');
66
                    }
67
                        
68
                    do {
69
                        
70
                        $this->out();
71
						
72
                        $subdomain = $this->in('Subdomain:');
73
						$valid = $this->_validateSubdomain($subdomain);
74
75
                        $this->out();
76
						
77
                        if ($valid) {
78
                            $subdomains[] = $subdomain;
79
                        } else {
80
                            $this->err('Invalid Subdomain.');
81
                        }
82
                        
83
                    } while (!$valid || $this->_askAddSubdomain());
84
                    
85
                }
86
                
87
                $subdomains = array_values(array_unique($subdomains));
88
                
89
                $subdomains = $this->_modifyArray($subdomains);
90
                
91
				$this->_currentSubdomains($subdomains);
92
                            
93
                while ($this->_countSubdomains($subdomains) && $this->_askDeleteSubdomain()) {
94
                    
95
                    $this->out();
96
                    $deleteKey = (int) $this->in('Enter Number to Delete:', array_keys($subdomains));
97
                    
98
					$this->_deleteSubdomain($subdomains, $deleteKey);
99
                    
100
                }
101
                
102
                Configure::write('Multidimensional/Subdomains.subdomains', array_values($subdomains));
103
                Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);
104
                
105
                if (!$this->_countSubdomains($subdomains)) {
106
                    $this->out();
107
                    $this->err('No Subdomains Configured.', 2);                    
108
                }
109
                
110
            } while (count($subdomains) == 0 && $this->_askStartOver());
111
            
112
            $this->out();
113
            if ($this->_countSubdomains($subdomains)) {
114
                $this->out('Configuration Saved!', 2);
115
            } else {
116
                $this->err('Plugin Not Currently Active.', 2);    
117
            }
118
        }
119
    
120
    }
121
    
122
    private function _modifyArray (array $array) {
123
124
        return array_combine(range(1, count($array)), array_values($array)); ;    
125
        
126
    }
127
    
128
	
129
	private function _currentSubdomains (array $subdomains) {
130
		
131
		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 true; 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...
132
			
133
			$this->out();
134
			$this->out('Current Subdomains:', 2);
135
				
136
			foreach ($subdomains AS $key => $value) {
137
				$this->out(' ' . ($key) . '. ' . $value);    
138
			}                
139
			
140
			$this->out();
141
		
142
		}
143
		
144
	}
145
	
146
	private function _askAddSubdomain () {
147
	
148
		return (strtolower($this->in('Add Subdomain?', ['y', 'n'])) === 'y');
149
		
150
	}
151
	
152
	private function _askDeleteSubdomain () {
153
	
154
		return (strtolower($this->in('Delete Subdomain?', ['y', 'n'])) === 'y');
155
		
156
	}
157
	
158
	private function _deleteSubdomain (&$subdomains, $key) {
159
		
160
		if (isset($subdomains[$key])) {
161
			$this->out();
162
			$this->out('Deleted: ' . $subdomains[$key], 2);
163
			unset($subdomains[$key]);
164
			return true; 
165
		}
166
		
167
		return false;
168
		
169
	}
170
	
171
	private function _validateSubdomain ($subdomain) {
172
	
173
		return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
174
		
175
	}
176
	
177
	private function _askStartOver () {
178
		
179
		return (strtolower($this->in('Start Over?', ['y', 'n'])) === 'y');
180
		
181
	}
182
	
183
	private function _countSubdomains ($subdomains) {
184
		
185
		if (!is_array($subdomains)) {
186
			return false;								
187
		}
188
		
189
		return count($subdomains);		
190
		
191
	}
192
      
193
}