Passed
Push — master ( b500c7...b9401d )
by Alexey
02:14
created

MaintenanceController::actionEnable()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 30
rs 8.6506
cc 7
nc 6
nop 0
1
<?php
2
3
namespace dominus77\maintenance\commands;
4
5
use dominus77\maintenance\BackendMaintenance;
6
use yii\helpers\Console;
7
use yii\console\Controller;
8
use yii\base\Module;
9
use dominus77\maintenance\Maintenance;
10
use dominus77\maintenance\models\FileStateForm;
11
use dominus77\maintenance\models\SubscribeForm;
12
use dominus77\maintenance\interfaces\StateInterface;
13
14
/**
15
 * Maintenance mode
16
 * @package dominus77\maintenance\commands
17
 *
18
 * @property FileStateForm $fileStateForm
19
 */
20
class MaintenanceController extends Controller
21
{
22
    /**
23
     * Date
24
     * @var string
25
     */
26
    public $date;
27
    /**
28
     * Title
29
     * @var string
30
     */
31
    public $title;
32
    /**
33
     * Content
34
     * @var string
35
     */
36
    public $content;
37
    /**
38
     * @var string
39
     */
40
    public $subscribe;
41
42
    /**
43
     * @var string
44
     */
45
    public $timer;
46
47
    /**
48
     * @var StateInterface
49
     */
50
    protected $state;
51
    /**
52
     * @var mixed string
53
     */
54
    protected $exampleData;
55
56
    /**
57
     * MaintenanceController constructor.
58
     * @param string $id
59
     * @param Module $module
60
     * @param StateInterface $state
61
     * @param array $config
62
     */
63
    public function __construct($id, Module $module, StateInterface $state, array $config = [])
64
    {
65
        $this->state = $state;
66
        $this->exampleData = $this->exampleDateFormat();
67
        parent::__construct($id, $module, $config);
68
    }
69
70
71
    /**
72
     * Options
73
     * @param string $actionID
74
     * @return array|string[]
75
     */
76
    public function options($actionID)
77
    {
78
        return [
79
            'date',
80
            'title',
81
            'content',
82
            'subscribe',
83
            'timer'
84
        ];
85
    }
86
87
    /**
88
     * Aliases
89
     * @return array
90
     */
91
    public function optionAliases()
92
    {
93
        return [
94
            'd' => 'date',
95
            't' => 'title',
96
            'c' => 'content',
97
            's' => 'subscribe',
98
            'tm' => 'timer'
99
        ];
100
    }
101
102
    /**
103
     * Maintenance status or commands
104
     */
105
    public function actionIndex()
106
    {
107
        $stateForm = new FileStateForm();
108
        $subscribeForm = new SubscribeForm();
109
110
        if ($this->state->isEnabled()) {
111
            $enabled = $this->ansiFormat('ENABLED', Console::FG_RED);
112
            $datetime = $stateForm->getDateTime();
113
            $this->stdout("Maintenance Mode has been $enabled\n");
114
            $this->stdout("on until $datetime\n");
115
            $this->stdout('Total (' . count($subscribeForm->getEmails()) . ') followers.' . PHP_EOL);
116
117
            $this->stdout("\nMaintenance Mode update date and time.\n");
118
            $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
119
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
120
121
            $this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n");
122
            $this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n");
123
124
            $this->stdout("\nMaintenance Mode disable.\n");
125
            $this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n");
126
        } else {
127
            $disabled = $this->ansiFormat('DISABLED', Console::FG_GREEN);
128
            $this->stdout("Maintenance Mode has been $disabled!\n");
129
130
            $this->stdout("\nMaintenance Mode enable.\n");
131
            $this->stdout("Use:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
132
133
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
134
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
135
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
136
137
            $this->stdout("\nMaintenance Mode update date and time.\n");
138
            $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
139
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
140
141
            $this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n");
142
            $this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n");
143
144
            $this->stdout("\nMaintenance Mode disable.\n");
145
            $this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n");
146
        }
147
    }
148
149
    /**
150
     * Enable maintenance mode
151
     */
152
    public function actionEnable()
153
    {
154
        $stateForm = new FileStateForm();
155
        if (!$this->state->isEnabled()) {
156
            $stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE;
157
            $stateForm = $this->setFileStateForm($stateForm);
158
            if ($stateForm->validate()) {
159
                if ($stateForm->title === null && $this->title === null) {
160
                    $stateForm->title = BackendMaintenance::t('app', $stateForm->getDefaultTitle());
161
                }
162
                if ($stateForm->text === null && $this->content === null) {
163
                    $stateForm->text = BackendMaintenance::t('app', $stateForm->getDefaultText());
164
                }
165
                $stateForm->save();
166
            }
167
        }
168
        $datetime = $stateForm->getDateTime();
169
        $enabled = $this->ansiFormat('ENABLED', Console::FG_RED);
170
        $this->stdout("Maintenance Mode has been $enabled\n");
171
        $this->stdout("on until $datetime\n");
172
173
        $this->stdout("\nMaintenance Mode update date and time.\n");
174
        $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
175
        $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
176
177
        $this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n");
178
        $this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n");
179
180
        $this->stdout("\nMaintenance Mode disable.\n");
181
        $this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n");
182
    }
183
184
    /**
185
     * Update date and time maintenance mode
186
     */
187
    public function actionUpdate()
188
    {
189
        $stateForm = new FileStateForm();
190
        if ($this->state->isEnabled()) {
191
            $stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE;
192
            $stateForm = $this->setFileStateForm($stateForm);
193
            if ($stateForm->validate()) {
194
                $stateForm->save();
195
                $updated = $this->ansiFormat('UPDATED', Console::FG_GREEN);
196
                $this->stdout("Maintenance Mode has been $updated!\n");
197
            } else {
198
                $this->stdout("Not specified what to update\n");
199
                $this->stdout("\nUse:\n");
200
                $this->stdout("\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
201
                $this->stdout("\nphp yii maintenance/update --title=\"Maintenance\"\nto update maintenance mode title.\n");
202
                $this->stdout("\nphp yii maintenance/update --content=\"Maintenance\"\nto update maintenance mode text content.\n");
203
                $this->stdout("\nphp yii maintenance/update --subscribe=true\nto enable subscribe form for maintenance mode.\n");
204
                $this->stdout("\nphp yii maintenance/update --timer=true\nto enable count down timer form for maintenance mode.\n");
205
            }
206
        } else {
207
            $this->stdout("Maintenance Mode not enable!\n");
208
209
            $this->stdout("Use:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
210
211
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
212
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
213
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
214
        }
215
    }
216
217
    /**
218
     * Disable maintenance mode
219
     */
220
    public function actionDisable()
221
    {
222
        $stateForm = new FileStateForm();
223
        $this->stdout("Maintenance Mode has been disabled.\n");
224
        if ($stateForm->isEnabled()) {
225
            $stateForm->disable();
226
            $subscribeForm = new SubscribeForm();
227
            $result = $subscribeForm->send();
228
            if ($result || $result === 0) {
229
                $this->stdout("Notified ($result) subscribers.\n");
230
            }
231
        }
232
233
        $this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
234
235
        $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
236
        $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
237
        $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
238
    }
239
240
    /**
241
     * Show subscribers to whom messages
242
     */
243
    public function actionFollowers()
244
    {
245
        $stateForm = new FileStateForm();
246
        $subscribeForm = new SubscribeForm();
247
        if (!$stateForm->isEnabled()) {
248
            $this->stdout("Maintenance Mode not enable!\n");
249
250
            $this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
251
252
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
253
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
254
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
255
        } else if ($emails = $subscribeForm->getEmails()) {
256
            $this->stdout('Total (' . count($emails) . ') followers:' . PHP_EOL);
257
            foreach ($emails as $email) {
258
                $this->stdout($email . PHP_EOL);
259
            }
260
        } else {
261
            $this->stdout("No followers\n");
262
        }
263
    }
264
265
    /**
266
     * @param FileStateForm $stateForm
267
     * @return FileStateForm
268
     */
269
    protected function setFileStateForm(FileStateForm $stateForm)
270
    {
271
        if ($this->date) {
272
            $stateForm->date = $this->date;
273
        }
274
        if ($this->title) {
275
            $stateForm->title = $this->title;
276
        }
277
        if ($this->content) {
278
            $stateForm->text = $this->content;
279
        }
280
        if ($this->subscribe) {
281
            $stateForm->subscribe = $this->subscribe === 'true';
282
        }
283
        if ($this->timer) {
284
            $stateForm->countDown = $this->timer === 'true';
285
        }
286
        return $stateForm;
287
    }
288
289
    /**
290
     * Example format date time
291
     * @return mixed
292
     */
293
    protected function exampleDateFormat()
294
    {
295
        return date($this->state->getDateFormat());
296
    }
297
}
298
299