Completed
Push — master ( cbe5fa...f32689 )
by AJ
01:52
created

SubdomainsInstallShell::_inputYesNo()   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->_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->_displayCurrentUniqueSubdomains($subdomains);
60
                            
61
                $this->_deleteSubdomain($subdomains);
62
                
63
                $this->_writeConfig($subdomains);
64
                $this->_finalCheck($subdomains);
65
                
66
            } while (!$this->_countSubdomains($subdomains) && $this->_inputYesNo('Start over?'));
67
            
68
            $this->_displayFinal($subdomains);
69
70
        }
71
    
72
    }
73
    
74
    private function _displayCurrentUniqueSubdomains(&$subdomains) {
0 ignored issues
show
Coding Style introduced by
function _displayCurrentUniqueSubdomains() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
75
        
76
        $subdomains = $this->_uniqueSubdomains($subdomains);
77
        $subdomains = $this->_modifyArray($subdomains);
78
        $this->_displayCurrentSubdomains($subdomains);
79
        
80
    }
81
    
82
    private function _inputSubdomain(&$subdomains) {
0 ignored issues
show
Coding Style introduced by
function _inputSubdomain() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
83
    
84
        do {
85
        
86
            $this->out();
87
            $subdomain = strtolower($this->in('Subdomain:'));
88
            $valid = $this->_validateSubdomain($subdomain);
89
            $this->out();
90
            
91
            if ($valid) {
92
                $subdomains[] = $subdomain;
93
            } else {
94
                $this->err('Invalid subdomain.');
95
            }
96
            
97
        } while (!$valid || $this->_inputYesNo('Add a subdomain?'));            
98
        
99
    }
100
        
101
    private function _uniqueSubdomains($subdomains) {
0 ignored issues
show
Coding Style introduced by
function _uniqueSubdomains() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
102
        
103
        return array_values(array_unique($subdomains));
104
        
105
    }
106
        
107
    private function _writeConfig($subdomains) {
0 ignored issues
show
Coding Style introduced by
function _writeConfig() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
108
        
109
        Configure::write('Multidimensional/Subdomains.subdomains', array_values($subdomains));
110
        Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);    
111
        
112
    }
113
    
114
    private function _displayFinal($subdomains) {
0 ignored issues
show
Coding Style introduced by
function _displayFinal() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
115
    
116
        $this->out();
117
        if ($this->_countSubdomains($subdomains)) {
118
            $this->out('Configuration saved!', 2);
119
        } else {
120
            $this->err('Plugin not currently active.', 2);    
121
        }
122
        
123
    }
124
    
125
    private function _finalCheck($subdomains) {
0 ignored issues
show
Coding Style introduced by
function _finalCheck() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
126
    
127
        if (!$this->_countSubdomains($subdomains)) {
128
            $this->out();
129
            $this->err('No subdomains configured.', 2);                    
130
        }    
131
        
132
    }
133
    
134
    private function _getSubdomains() {
0 ignored issues
show
Coding Style introduced by
function _getSubdomains() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
135
    
136
        $check = Configure::check('Multidimensional/Subdomains.subdomains');
137
        
138
        if (!$check) {
139
            return false;    
140
        }
141
        
142
        $subdomains = Configure::consume('Multidimensional/Subdomains.subdomains');
143
        
144
        if ($this->_countSubdomains($subdomains)) {
145
            return $subdomains;
146
        }
147
        
148
        return false;
149
        
150
    }
151
    
152
    private function _modifyArray(array $array) {
0 ignored issues
show
Coding Style introduced by
function _modifyArray() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
153
154
        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...
155
        
156
    }
157
    
158
    
159
    private function _displayCurrentSubdomains(array $subdomains) {
0 ignored issues
show
Coding Style introduced by
function _displayCurrentSubdomains() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
160
        
161
        if ($this->_countSubdomains($subdomains)) {
162
            
163
            $this->out();
164
            $this->out('Current subdomains:', 2);
165
                
166
            foreach ($subdomains AS $key => $value) {
167
                $this->out(' ' . ($key) . '. ' . $value);    
168
            }                
169
            
170
            $this->out();
171
        
172
        }
173
        
174
    }
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 (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
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) {
0 ignored issues
show
Coding Style introduced by
function _deleteSubdomain() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
183
        
184
        while ($this->_countSubdomains($subdomains) && $this->_inputYesNo('Delete a subdomain?')) {
185
186
            $this->out();
187
            $key = (int) $this->in('Enter number to delete:', array_keys($subdomains)); 
188
            
189
            if (isset($subdomains[$key])) {
190
                $this->out();
191
                $this->out('Deleted: ' . $subdomains[$key], 2);
192
                unset($subdomains[$key]);
193
            }
194
        
195
        }
196
                
197
    }
198
    
199
    /**
200
     * @param string|null $subdomain
201
     */    
202
    private function _validateSubdomain($subdomain) {
0 ignored issues
show
Coding Style introduced by
function _validateSubdomain() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
203
    
204
        if (is_null($subdomain) || empty($subdomain)) {
205
            return false;
206
        }
207
    
208
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
209
        
210
    }
211
        
212
    private function _countSubdomains($subdomains) {
0 ignored issues
show
Coding Style introduced by
function _countSubdomains() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

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...
213
        
214
        if (!is_array($subdomains)) {
215
            return false;                                
216
        }
217
        
218
        if (is_null($subdomains)) {
219
            return false;    
220
        }
221
        
222
        if (count($subdomains) === 0) {
223
            return false;
224
        }
225
        
226
        return (int) count($subdomains);        
227
        
228
    }
229
      
230
}