Completed
Push — master ( 1f25f1...686c3b )
by AJ
02:09
created

SubdomainsInstallShell::main()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 14
nc 2
nop 0
crap 20
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\Console\Shell;
19
use Cake\Core\Configure;
20
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware;
21
22
class SubdomainsInstallShell extends Shell
23
{
24
25
    /**
26
     * @return void
27
     */
28
    public function main()
29
    {
30
        $this->clear();
31
32
        $this->helper('Multidimensional/Subdomains.Header')->output();
33
34
        $subdomains = $this->_getSubdomains();
35
        $continue = $this->_runProgram($subdomains);
36
37
        if ($continue) {
38
            do {
39
                $this->_inputSubdomain($subdomains);
40
                $this->_displayCurrentUniqueSubdomains($subdomains);
41
                $this->_deleteSubdomain($subdomains);
42
                $this->_writeConfig($subdomains);
43
                $this->_finalCheck($subdomains);
44
            } while (!$this->_countSubdomains($subdomains) && $this->_inputYesNo('Start over?'));
45
        }
46
47
        $this->_displayFinal($subdomains);
48
    }
49
50
    /**
51
     * @param array $subdomains
52
     * @return bool
53
     */
54
    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...
55
    {
56
        if ($this->_countSubdomains($subdomains)) {
57
            return $this->_inputYesNo('Update configuration?');
58
        } else {
59
            return $this->_inputYesNo('Install subdomains plugin?');
60
        }
61
    }
62
63
    /**
64
     * @param array $subdomains
65
     * @return void
66
     */
67
    private function _displayCurrentUniqueSubdomains(&$subdomains)
68
    {
69
        if ($this->_countSubdomains($subdomains)) {
70
            $subdomains = $this->_uniqueSubdomains($subdomains);
71
            $subdomains = $this->_modifyArray($subdomains);
72
            $this->_displayCurrentSubdomains($subdomains);
73
        }
74
    }
75
76
    /**
77
     * @param array $subdomains
78
     * @return void
79
     */
80
    private function _inputSubdomain(&$subdomains)
81
    {
82
        $valid = true;
83
        $this->out();
84
85
        while (!$valid || $this->_inputYesNo('Add a subdomain?')) {
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
    }
98
99
    /**
100
     * @param array $subdomains
101
     * @return array $subdomains
102
     */
103
    private function _uniqueSubdomains($subdomains)
104
    {
105
        if ($this->_countSubdomains($subdomains)) {
106
            return array_values(array_unique($subdomains));
107
        } else {
108
            return $subdomains;
109
        }
110
    }
111
112
    /**
113
     * @param array $subdomains
114
     * @return void
115
     */
116
    private function _writeConfig($subdomains)
117
    {
118
        Configure::write('Multidimensional/Subdomains.Subdomains', array_values($subdomains));
119
        Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']);
120
    }
121
122
    /**
123
     * @param array $subdomains
124
     * @return void
125
     */
126
    private function _displayFinal($subdomains)
127
    {
128
        $this->out();
129
        if ($this->_countSubdomains($subdomains)) {
130
            $this->out('Configuration saved!', 2);
131
        } else {
132
            $this->err('Plugin not currently active.', 2);
133
        }
134
    }
135
136
    /**
137
     * @param array $subdomains
138
     * @return void
139
     */
140
    private function _finalCheck($subdomains)
141
    {
142
        if (!$this->_countSubdomains($subdomains)) {
143
            $this->out();
144
            $this->err('No subdomains configured.', 2);
145
        }
146
    }
147
148
    /**
149
     * @return SubdomainMiddleware
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
150
     */
151
    private function _getSubdomains()
152
    {
153
        $subdomainObject = new SubdomainMiddleware();
154
155
        return $subdomainObject->getSubdomains();
156
    }
157
158
    /**
159
     * @param array $subdomains
160
     * @return array
161
     */
162
    private function _modifyArray(array $subdomains)
163
    {
164
        if ($this->_countSubdomains($subdomains)) {
165
            return array_combine(
166
                range(1, count($subdomains)),
167
                array_values($subdomains)
168
            );
169
        } else {
170
            return $subdomains;
171
        }
172
    }
173
174
    /**
175
     * @param array $subdomains
176
     * @return void
177
     */
178
    private function _displayCurrentSubdomains(array $subdomains)
179
    {
180
     if ($this->_countSubdomains($subdomains)) {
181
            $this->out();
182
            $this->out('Current subdomains:', 2);
183
184
            foreach ($subdomains as $key => $value) {
185
                $this->out(' ' . ($key) . '. ' . $value);
186
            }
187
188
            $this->out();
189
        }
190
    }
191
192
    /**
193
     * @param string $string
194
     * @return bool
195
     */
196
    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...
197
    {
198
        return strtolower($this->in($string, ['y', 'n'])) === 'y';
199
    }
200
201
    private function _deleteSubdomain(&$subdomains)
202
    {
203
        while ($this->_countSubdomains($subdomains) && $this->_inputYesNo('Delete a subdomain?')) {
204
            $this->out();
205
            $key = (int)$this->in('Enter number to delete:', array_keys($subdomains));
206
207
            if (isset($subdomains[$key])) {
208
                $this->out();
209
                $this->out('Deleted: ' . $subdomains[$key], 2);
210
                unset($subdomains[$key]);
211
            }
212
        }
213
    }
214
215
    /**
216
     * @param  string|null $subdomain
217
     * @return string|null
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
218
     */
219
    private function _validateSubdomain($subdomain)
220
    {
221
        if (is_null($subdomain) || empty($subdomain)) {
222
            return false;
223
        }
224
225
        return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain);
226
    }
227
228
    /**
229
     * @param  array $subdomains
230
     * @return int
231
     */
232
    private function _countSubdomains($subdomains)
233
    {
234
        if (!isset($subdomains) || !is_array($subdomains)) {
235
            return 0;
236
        }
237
238
        return count($subdomains);
239
    }
240
}
241