Completed
Push — master ( aeb2ba...1666d5 )
by AJ
04:57
created

SubdomainsInstallShell::_runProgram()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware;
22
23
class SubdomainsInstallShell extends Shell {
24
    
25
    public function main() {
26
        
27
        $this->clear();
28
        
29
        $this->helper('Multidimensional/Subdomains.Header')->output();
30
    
31
        $subdomains = $this->_getSubdomains();    
32
        $continue = $this->_runProgram($subdomains);
33
                  
34
        if ($continue) {
35
            
36
            do {
37
                
38
                $this->_inputSubdomain($subdomains);  
39
                $this->_displayCurrentUniqueSubdomains($subdomains);          
40
                $this->_deleteSubdomain($subdomains);
41
                $this->_writeConfig($subdomains);
42
                $this->_finalCheck($subdomains);
43
                
44
            } while (!$this->_countSubdomains($subdomains) && $this->_inputYesNo('Start over?'));
45
46
        }
47
        
48
        $this->_displayFinal($subdomains);
49
    
50
    }
51
    
52
    private function _runProgram($subdomains) {
0 ignored issues
show
Coding Style introduced by
function _runProgram() 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...
53
                
54
        if ($this->_countSubdomains($subdomains)) {
55
            return $this->_inputYesNo('Update configuration?');    
56
        } else {    
57
            return $this->_inputYesNo('Install subdomains plugin?');    
58
        }
59
        
60
    }
61
    
62
    private function _displayCurrentUniqueSubdomains(&$subdomains) {
63
     
64
         if ($this->_countSubdomains($subdomains)) {   
65
            $subdomains = $this->_uniqueSubdomains($subdomains);
66
            $subdomains = $this->_modifyArray($subdomains);
67
            $this->_displayCurrentSubdomains($subdomains);
68
         }
69
        
70
    }
71
    
72
    private function _inputSubdomain(&$subdomains) {
73
    
74
        $valid = true;
75
        $this->out();
76
    
77
        while (!$valid || $this->_inputYesNo('Add a subdomain?')) {
78
        
79
            $this->out();
80
            $subdomain = strtolower($this->in('Subdomain:'));
81
            $valid = $this->_validateSubdomain($subdomain);
82
            $this->out();
83
            
84
            if ($valid) {
85
                $subdomains[] = $subdomain;
86
            } else {
87
                $this->err('Invalid subdomain.');
88
            }
89
            
90
        };            
91
        
92
    }
93
        
94
    private function _uniqueSubdomains($subdomains) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
95
        
96
        if ($this->_countSubdomains($subdomains)) {
97
            return array_values(array_unique($subdomains));
98
        } else {
99
            return $subdomains;
100
        }
101
        
102
    }
103
        
104
    private function _writeConfig($subdomains) {
105
        
106
        Configure::write('Multidimensional/Subdomains.subdomains', array_values($subdomains));
107
        Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);    
108
        
109
    }
110
    
111
    private function _displayFinal($subdomains) {
112
    
113
        $this->out();
114
        if ($this->_countSubdomains($subdomains)) {
115
            $this->out('Configuration saved!', 2);
116
        } else {
117
            $this->err('Plugin not currently active.', 2);    
118
        }
119
        
120
    }
121
    
122
    private function _finalCheck($subdomains) {
123
    
124
        if (!$this->_countSubdomains($subdomains)) {
125
            $this->out();
126
            $this->err('No subdomains configured.', 2);                    
127
        }    
128
        
129
    }
130
    
131
    private function _getSubdomains() {
132
        
133
        $subdomainObject = new SubdomainMiddleware();
134
        return $subdomainObject->getSubdomains();
135
        
136
    }
137
    
138
    private function _modifyArray(array $subdomains) {
139
        
140
        if ($this->_countSubdomains($subdomains)) {
141
        
142
            return array_combine(
143
                range(1, count($subdomains)),
144
                array_values($subdomains));
145
                
146
        } else {
147
            return $subdomains;    
148
        }
149
        
150
    }
151
    
152
    
153
    private function _displayCurrentSubdomains(array $subdomains) {
154
        
155
        if ($this->_countSubdomains($subdomains)) {
156
            
157
            $this->out();
158
            $this->out('Current subdomains:', 2);
159
                
160
            foreach ($subdomains AS $key => $value) {
161
                $this->out(' ' . ($key) . '. ' . $value);    
162
            }                
163
            
164
            $this->out();
165
        
166
        }
167
        
168
    }
169
    
170
    /**
171
     * @param string $string
172
     */     
173
    private function _inputYesNo($string) {
0 ignored issues
show
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...
174
    
175
        return strtolower($this->in($string, ['y', 'n'])) === 'y';    
176
        
177
    }
178
179
    private function _deleteSubdomain(&$subdomains) {
180
        
181
        while ($this->_countSubdomains($subdomains) && $this->_inputYesNo('Delete a subdomain?')) {
182
183
            $this->out();
184
            $key = (int) $this->in('Enter number to delete:', array_keys($subdomains)); 
185
            
186
            if (isset($subdomains[$key])) {
187
                $this->out();
188
                $this->out('Deleted: ' . $subdomains[$key], 2);
189
                unset($subdomains[$key]);
190
            }
191
        
192
        }
193
                
194
    }
195
    
196
    /**
197
     * @param string|null $subdomain
198
     */    
199
    private function _validateSubdomain($subdomain) {
200
    
201
        if (is_null($subdomain) || empty($subdomain)) {
202
            return false;
203
        }
204
    
205
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
206
        
207
    }
208
    
209
    private function _countSubdomains($subdomains) {
210
       
211
        if (!isset($subdomains) || !is_array($subdomains)) {
212
            return 0;
213
        }
214
        
215
        return count($subdomains);
216
        
217
    }
218
      
219
}