Passed
Push — master ( 84eed8...6d6bcb )
by Alexey
02:52
created

ConsoleController::mbLcFirst()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
namespace dominus77\maintenance\base;
4
5
use Yii;
6
use yii\console\Controller;
7
use yii\helpers\Console;
8
use dominus77\maintenance\Maintenance;
9
use dominus77\maintenance\models\FileStateForm;
10
use dominus77\maintenance\models\SubscribeForm;
11
12
/**
13
 * Class ConsoleController
14
 * @package dominus77\maintenance\base
15
 */
16
class ConsoleController extends Controller
17
{
18
    /**
19
     * @var mixed string
20
     */
21
    protected $exampleData;
22
    /**
23
     * @var FileStateForm
24
     */
25
    protected $stateForm;
26
    /**
27
     * @var SubscribeForm
28
     */
29
    protected $subscribeForm;
30
    /**
31
     * @var string
32
     */
33
    protected $enabled;
34
    /**
35
     * @var string
36
     */
37
    protected $disabled;
38
    /**
39
     * @var string
40
     */
41
    protected $enabledMode;
42
    /**
43
     * @var string
44
     */
45
    protected $updatedMode;
46
    /**
47
     * @var string
48
     */
49
    protected $notUpdatedMode;
50
    /**
51
     * @var string
52
     */
53
    protected $disabledMode;
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function init()
59
    {
60
        parent::init();
61
        $this->stateForm = new FileStateForm();
62
        $this->subscribeForm = new SubscribeForm();
63
        $this->exampleData = $this->exampleDateFormat();
64
        $this->setMessageVales();
65
    }
66
67
    /**
68
     * Set Message Values
69
     */
70
    protected function setMessageVales()
71
    {
72
        $this->enabledMode = $this->ansiFormat(Maintenance::t('app', 'ENABLED'), Console::FG_RED);
73
        $this->updatedMode = $this->ansiFormat(Maintenance::t('app', 'UPDATED'), Console::FG_GREEN);
74
        $this->notUpdatedMode = $this->ansiFormat(Maintenance::t('app', 'NOT UPDATED'), Console::FG_YELLOW);
75
        $this->disabledMode = $this->ansiFormat(Maintenance::t('app', 'DISABLED'), Console::FG_GREEN);
76
        $this->enabled = $this->ansiFormat(Maintenance::t('app', 'ENABLED'), Console::FG_GREEN);
77
        $this->disabled = $this->ansiFormat(Maintenance::t('app', 'DISABLED'), Console::FG_RED);
78
    }
79
80
    /**
81
     * Render is Disabled
82
     */
83
    protected function renderGroupDisabled()
84
    {
85
        $status = $this->disabledMode;
86
        $this->renderMaintenanceModeHasBeenStatus($status);
87
        $this->stdout(PHP_EOL);
88
89
        $this->renderEnableMaintenanceMode();
90
        $this->stdout(PHP_EOL);
91
        $this->stdout(PHP_EOL);
92
93
        $this->renderOptionsTable();
94
    }
95
96
    /**
97
     * Render is Enabled
98
     */
99
    protected function renderGroupEnabled()
100
    {
101
        $status = $this->enabledMode;
102
        $this->renderMaintenanceModeHasBeenStatus($status);
103
        $this->stdout(PHP_EOL);
104
105
        $this->renderOnUntilDateTime();
106
        $this->stdout(PHP_EOL);
107
108
        $this->renderSubscriptionInfo();
109
        $this->stdout(PHP_EOL);
110
        $this->stdout(PHP_EOL);
111
112
        $this->renderCountDownStatus();
113
        $this->stdout(PHP_EOL);
114
115
        $this->renderSubscriptionFormStatus();
116
        $this->stdout(PHP_EOL);
117
118
        $this->renderDisableAndSubscribe();
119
        $this->stdout(PHP_EOL);
120
121
        $this->renderUpdateMaintenanceMode();
122
        $this->stdout(PHP_EOL);
123
        $this->stdout(PHP_EOL);
124
125
        $this->renderOptionsTable();
126
    }
127
128
    /**
129
     * Maintenance Mode has been ENABLED/DISABLED
130
     *
131
     * @param $status string
132
     */
133
    protected function renderMaintenanceModeHasBeenStatus($status)
134
    {
135
        $message = Maintenance::t('app', 'Maintenance Mode has been {:status}', [
136
            ':status' => $status,
137
        ]);
138
        $this->stdout($message);
139
    }
140
141
    /**
142
     * Notified 2 subscribers.
143
     *
144
     * @param $count int
145
     */
146
    protected function renderNotifiedSubscribers($count = 0)
147
    {
148
        $this->stdout(Maintenance::t('app', '{n, plural, =0{No subscribers} =1{Notified one subscriber} other{Notified # subscribers}}.', [
149
            'n' => $count
150
        ]));
151
    }
152
153
    /**
154
     * on until 09-03-2020 11:15:04
155
     */
156
    protected function renderOnUntilDateTime()
157
    {
158
        $datetime = $this->stateForm->getDateTime();
159
        $message = Maintenance::t('app', 'on until {:datetime}', [
160
            ':datetime' => $datetime
161
        ]);
162
        $this->stdout($message);
163
    }
164
165
    /**
166
     * Total 2 followers.
167
     */
168
    protected function renderSubscriptionInfo()
169
    {
170
        $count = count($this->subscribeForm->getEmails());
171
        $message = Maintenance::t('app', '{n, plural, =0{No subscribers} =1{Total one subscriber} other{Total # subscribers}}.', [
172
            'n' => $count
173
        ]);
174
        $this->stdout($message);
175
    }
176
177
    /**
178
     * Count Down: ENABLED
179
     */
180
    protected function renderCountDownStatus()
181
    {
182
        $status = $this->stateForm->isTimer() ? $this->enabled : $this->disabled;
183
        $message = Maintenance::t('app', 'Count Down: {:status}', [
184
            ':status' => $status
185
        ]);
186
        $this->stdout($message);
187
    }
188
189
    /**
190
     * Subscription form: ENABLED
191
     */
192
    protected function renderSubscriptionFormStatus()
193
    {
194
        $status = $this->stateForm->isSubscribe() ? $this->enabled : $this->disabled;
195
        $message = Maintenance::t('app', 'Subscription form: {:status}', [
196
            ':status' => $status
197
        ]);
198
        $this->stdout($message);
199
    }
200
201
    /**
202
     * To turn off and notify subscribers,
203
     * use:
204
     * php yii maintenance/disable
205
     */
206
    protected function renderDisableAndSubscribe()
207
    {
208
        $message = Maintenance::t('app', "To turn off and notify subscribers,\nuse:");
209
        $this->stdout(PHP_EOL . $message . PHP_EOL);
210
        $message = 'php yii maintenance/disable';
211
        $this->stdout($message);
212
    }
213
214
    /**
215
     * To enable the maintenance mode,
216
     * use:
217
     * php yii maintenance/enable --option='value'
218
     */
219
    protected function renderEnableMaintenanceMode()
220
    {
221
        $message = Maintenance::t('app', "To enable the maintenance mode,\nuse:");
222
        $this->stdout(PHP_EOL . $message . PHP_EOL);
223
224
        $option = $this->mbLcFirst(trim(Maintenance::t('app', 'Option')));
225
        $value = $this->mbLcFirst(trim(Maintenance::t('app', 'Value')));
226
        $message = Maintenance::t('app', "php yii maintenance/enable --{:option}1='{:value}1' --{:option}2='{:value}2' ...", [
227
            ':option' => $option,
228
            ':value' => $value
229
        ]);
230
        $this->stdout($message);
231
    }
232
233
    /**
234
     * To update the maintenance mode,
235
     * use:
236
     * php yii maintenance/update --option='value'
237
     */
238
    protected function renderUpdateMaintenanceMode()
239
    {
240
        $message = Maintenance::t('app', "To update the maintenance mode,\nuse:");
241
        $this->stdout(PHP_EOL . $message . PHP_EOL);
242
243
        $option = $this->mbLcFirst(trim(Maintenance::t('app', 'Option')));
244
        $value = $this->mbLcFirst(trim(Maintenance::t('app', 'Value')));
245
        $message = Maintenance::t('app', "php yii maintenance/update --{:option}1='{:value}1' --{:option}2='{:value}2' ...", [
246
            ':option' => $option,
247
            ':value' => $value
248
        ]);
249
        $this->stdout($message);
250
    }
251
252
    /**
253
     * Options and aliases
254
     */
255
    protected function renderOptionsTable()
256
    {
257
        $option = Maintenance::t('app', 'Option');
258
        $alias = Maintenance::t('app', 'Alias');
259
        $value = Maintenance::t('app', 'Value');
260
        $exampleDat = $this->exampleData;
261
262
        $this->stdout('---------------------------------------------' . PHP_EOL);
263
        $this->stdout('|   ' . $option . '    | ' . $alias . ' | ' . $value . '            |' . PHP_EOL);
264
        $this->stdout('|=============|=======|=====================|' . PHP_EOL);
265
        $this->stdout('| --date      | -d    | ' . $exampleDat . ' |' . PHP_EOL);
266
        $this->stdout('| --title     | -t    | string              |' . PHP_EOL);
267
        $this->stdout('| --content   | -c    | string              |' . PHP_EOL);
268
        $this->stdout('| --subscribe | -s    | true/false          |' . PHP_EOL);
269
        $this->stdout('| --timer     | -tm   | true/false          |' . PHP_EOL);
270
        $this->stdout('---------------------------------------------' . PHP_EOL);
271
    }
272
273
    /**
274
     * @param $str string
275
     * @return string
276
     */
277
    protected function mbLcFirst($str = '')
278
    {
279
        if (is_string($str) && !empty($str)) {
280
            $charset = Yii::$app->charset;
281
            $first = mb_substr($str, 0, 1, $charset);
282
            $last = mb_substr($str, 1);
283
            $first = mb_strtolower($first, $charset);
284
            $last = mb_strtolower($last, $charset);
285
            return $first . $last;
286
        }
287
        return $str;
288
    }
289
290
    /**
291
     * Example format date time
292
     * @return mixed
293
     */
294
    protected function exampleDateFormat()
295
    {
296
        return date($this->stateForm->getDateFormat());
297
    }
298
}
299