Completed
Push — master ( de2947...fcd5f9 )
by AJ
02:25
created

SubdomainsInstallShell::_askInstall()   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();
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 (!$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...
38
                $first_run = true;
39
            }
40
            
41
        }
42
       
43
        if ($first_run ? $this->_askInstall() : $this->_askUpdate()) {
44
            
45
            do {
46
                
47
                $addMore = true;
48
                
49
                if ($first_run === false && $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...
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)) {
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...
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()) {
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...
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)) {
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...
106
                    $this->out();
107
                    $this->err('No Subdomains Configured.', 2);                    
108
                }
109
                
110
            } 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...
111
            
112
            $this->out();
113
            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...
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)) {
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 _askInstall() {
147
        
148
        return (strtolower($this->in('Install Subdomains Plugin?', ['y', 'n'])) === 'y');
149
        
150
    }
151
    
152
    private function _askUpdate() {
153
            
154
        return (strtolower($this->in('Update Configuration?', ['y', 'n'])) === 'y');
155
            
156
    }
157
158
    private function _askAddSubdomain() {
159
    
160
        return (strtolower($this->in('Add Subdomain?', ['y', 'n'])) === 'y');
161
        
162
    }
163
    
164
    private function _askDeleteSubdomain() {
165
    
166
        return (strtolower($this->in('Delete Subdomain?', ['y', 'n'])) === 'y');
167
        
168
    }
169
    
170
    /**
171
     * @param integer $key
172
     */
173
    private function _deleteSubdomain (&$subdomains, $key) {
174
        
175
        if (isset($subdomains[$key])) {
176
            
177
            $this->out();
178
            $this->out('Deleted: ' . $subdomains[$key], 2);
179
            unset($subdomains[$key]);
180
        
181
            return true; 
182
        
183
        }
184
        
185
        return false;
186
        
187
    }
188
    
189
    /**
190
     * @param string|null $subdomain
191
     */    
192
    private function _validateSubdomain ($subdomain) {
193
    
194
        if (is_null($subdomain) || empty($subdomain)) {
195
            return false;
196
        }
197
    
198
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
199
        
200
    }
201
    
202
    private function _askStartOver() {
203
        
204
        return (strtolower($this->in('Start Over?', ['y', 'n'])) === 'y');
205
        
206
    }
207
    
208
    private function _countSubdomains($subdomains) {
209
        
210
        if (!is_array($subdomains) || is_null($subdomains)) {
211
            return false;                                
212
        }
213
        
214
        return (int) count($subdomains);        
215
        
216
    }
217
      
218
}