Passed
Push — master ( 110caf...29f810 )
by Alexey
02:39
created

ConsoleController::renderOnUntilDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace dominus77\maintenance\base;
4
5
use Yii;
6
use yii\console\Controller;
7
use dominus77\maintenance\Maintenance;
8
use dominus77\maintenance\models\FileStateForm;
9
use dominus77\maintenance\models\SubscribeForm;
10
use yii\helpers\Console;
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
        $message = Maintenance::t('app', '{n, plural, =0{No subscribers} =1{Total one subscriber} other{Total # subscribers}}.', [
171
            'n' => count($this->subscribeForm->getEmails())
172
        ]);
173
        $this->stdout($message);
174
    }
175
176
    /**
177
     * Count Down: ENABLED
178
     */
179
    protected function renderCountDownStatus()
180
    {
181
        $status = $this->stateForm->isTimer() ? $this->enabled : $this->disabled;
182
        $message = Maintenance::t('app', 'Count Down: {:status}', [
183
            ':status' => $status
184
        ]);
185
        $this->stdout($message);
186
    }
187
188
    /**
189
     * Subscription form: ENABLED
190
     */
191
    protected function renderSubscriptionFormStatus()
192
    {
193
        $status = $this->stateForm->isSubscribe() ? $this->enabled : $this->disabled;
194
        $message = Maintenance::t('app', 'Subscription form: {:status}', [
195
            ':status' => $status
196
        ]);
197
        $this->stdout($message);
198
    }
199
200
    /**
201
     * To turn off and notify subscribers,
202
     * use:
203
     * php yii maintenance/disable
204
     */
205
    protected function renderDisableAndSubscribe()
206
    {
207
        $message = Maintenance::t('app', "To turn off and notify subscribers,\nuse:");
208
        $this->stdout(PHP_EOL . $message . PHP_EOL);
209
        $message = 'php yii maintenance/disable';
210
        $this->stdout($message);
211
    }
212
213
    /**
214
     * To enable the maintenance mode,
215
     * use:
216
     * php yii maintenance/enable --option='value'
217
     */
218
    protected function renderEnableMaintenanceMode()
219
    {
220
        $message = Maintenance::t('app', "To enable the maintenance mode,\nuse:");
221
        $this->stdout(PHP_EOL . $message . PHP_EOL);
222
223
        $option = Maintenance::t('app', 'Option');
224
        $value = Maintenance::t('app', 'Value');
225
        $message = Maintenance::t('app', "php yii maintenance/enable --{:option}1='{:value}1' --{:option}2='{:value}2' ...", [
226
            ':option' => $this->mb_lcfirst(trim($option)),
227
            ':value' => $this->mb_lcfirst(trim($value))
228
        ]);
229
        $this->stdout($message);
230
    }
231
232
    /**
233
     * To update the maintenance mode,
234
     * use:
235
     * php yii maintenance/update --option='value'
236
     */
237
    protected function renderUpdateMaintenanceMode()
238
    {
239
        $message = Maintenance::t('app', "To update the maintenance mode,\nuse:");
240
        $this->stdout(PHP_EOL . $message . PHP_EOL);
241
242
        $option = Maintenance::t('app', 'Option');
243
        $value = Maintenance::t('app', 'Value');
244
        $message = Maintenance::t('app', "php yii maintenance/update --{:option}1='{:value}1' --{:option}2='{:value}2' ...", [
245
            ':option' => $this->mb_lcfirst(trim($option)),
246
            ':value' => $this->mb_lcfirst(trim($value))
247
        ]);
248
        $this->stdout($message);
249
    }
250
251
    /**
252
     * Options and aliases
253
     */
254
    protected function renderOptionsTable()
255
    {
256
        $option = Maintenance::t('app', 'Option');
257
        $alias = Maintenance::t('app', 'Alias');
258
        $value = Maintenance::t('app', 'Value');
259
        $exampleDat = $this->exampleData;
260
261
        $this->stdout('---------------------------------------------' . PHP_EOL);
262
        $this->stdout('|   ' . $option . '    | ' . $alias . ' | ' . $value . '            |' . PHP_EOL);
263
        $this->stdout('|=============|=======|=====================|' . PHP_EOL);
264
        $this->stdout('| --date      | -d    | ' . $exampleDat . ' |' . PHP_EOL);
265
        $this->stdout('| --title     | -t    | string              |' . PHP_EOL);
266
        $this->stdout('| --content   | -c    | string              |' . PHP_EOL);
267
        $this->stdout('| --subscribe | -s    | true/false          |' . PHP_EOL);
268
        $this->stdout('| --timer     | -tm   | true/false          |' . PHP_EOL);
269
        $this->stdout('---------------------------------------------' . PHP_EOL);
270
    }
271
272
    /**
273
     * lcfirst()
274
     * @param $str string
275
     * @return string
276
     */
277
    protected function mb_lcfirst($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
}