Passed
Push — master ( b9401d...ca1c41 )
by Alexey
02:05
created

MaintenanceController   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 31
eloc 135
c 3
b 1
f 0
dl 0
loc 284
rs 9.92

11 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 8 1
A actionIndex() 0 41 2
A __construct() 0 5 1
A optionAliases() 0 8 1
A actionDisable() 0 18 4
A actionUpdate() 0 27 3
A setFileStateForm() 0 18 6
A exampleDateFormat() 0 3 1
A setDefaultValue() 0 7 5
A actionFollowers() 0 19 4
A actionEnable() 0 25 3
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
            $this->setDefaultValue($stateForm);
159
            if ($stateForm->validate()) {
160
                $stateForm->save();
161
            }
162
        }
163
        $datetime = $stateForm->getDateTime();
164
        $enabled = $this->ansiFormat('ENABLED', Console::FG_RED);
165
        $this->stdout("Maintenance Mode has been $enabled\n");
166
        $this->stdout("on until $datetime\n");
167
168
        $this->stdout("\nMaintenance Mode update date and time.\n");
169
        $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
170
        $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
171
172
        $this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n");
173
        $this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n");
174
175
        $this->stdout("\nMaintenance Mode disable.\n");
176
        $this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n");
177
    }
178
179
    /**
180
     * Update date and time maintenance mode
181
     */
182
    public function actionUpdate()
183
    {
184
        $stateForm = new FileStateForm();
185
        if ($this->state->isEnabled()) {
186
            $stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE;
187
            $stateForm = $this->setFileStateForm($stateForm);
188
            if ($stateForm->validate()) {
189
                $stateForm->save();
190
                $updated = $this->ansiFormat('UPDATED', Console::FG_GREEN);
191
                $this->stdout("Maintenance Mode has been $updated!\n");
192
            } else {
193
                $this->stdout("Not specified what to update\n");
194
                $this->stdout("\nUse:\n");
195
                $this->stdout("\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
196
                $this->stdout("\nphp yii maintenance/update --title=\"Maintenance\"\nto update maintenance mode title.\n");
197
                $this->stdout("\nphp yii maintenance/update --content=\"Maintenance\"\nto update maintenance mode text content.\n");
198
                $this->stdout("\nphp yii maintenance/update --subscribe=true\nto enable subscribe form for maintenance mode.\n");
199
                $this->stdout("\nphp yii maintenance/update --timer=true\nto enable count down timer form for maintenance mode.\n");
200
            }
201
        } else {
202
            $this->stdout("Maintenance Mode not enable!\n");
203
204
            $this->stdout("Use:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
205
206
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
207
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
208
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
209
        }
210
    }
211
212
    /**
213
     * Disable maintenance mode
214
     */
215
    public function actionDisable()
216
    {
217
        $stateForm = new FileStateForm();
218
        $this->stdout("Maintenance Mode has been disabled.\n");
219
        if ($stateForm->isEnabled()) {
220
            $stateForm->disable();
221
            $subscribeForm = new SubscribeForm();
222
            $result = $subscribeForm->send();
223
            if ($result || $result === 0) {
224
                $this->stdout("Notified ($result) subscribers.\n");
225
            }
226
        }
227
228
        $this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
229
230
        $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
231
        $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
232
        $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
233
    }
234
235
    /**
236
     * Show subscribers to whom messages
237
     */
238
    public function actionFollowers()
239
    {
240
        $stateForm = new FileStateForm();
241
        $subscribeForm = new SubscribeForm();
242
        if (!$stateForm->isEnabled()) {
243
            $this->stdout("Maintenance Mode not enable!\n");
244
245
            $this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
246
247
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
248
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
249
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
250
        } else if ($emails = $subscribeForm->getEmails()) {
251
            $this->stdout('Total (' . count($emails) . ') followers:' . PHP_EOL);
252
            foreach ($emails as $email) {
253
                $this->stdout($email . PHP_EOL);
254
            }
255
        } else {
256
            $this->stdout("No followers\n");
257
        }
258
    }
259
260
    /**
261
     * @param FileStateForm $stateForm
262
     * @return FileStateForm
263
     */
264
    protected function setFileStateForm(FileStateForm $stateForm)
265
    {
266
        if ($this->date) {
267
            $stateForm->date = $this->date;
268
        }
269
        if ($this->title) {
270
            $stateForm->title = $this->title;
271
        }
272
        if ($this->content) {
273
            $stateForm->text = $this->content;
274
        }
275
        if ($this->subscribe) {
276
            $stateForm->subscribe = $this->subscribe === 'true';
277
        }
278
        if ($this->timer) {
279
            $stateForm->countDown = $this->timer === 'true';
280
        }
281
        return $stateForm;
282
    }
283
284
    /**
285
     * @param FileStateForm $stateForm
286
     */
287
    protected function setDefaultValue(FileStateForm $stateForm)
288
    {
289
        if ($stateForm->title === null && $this->title === null) {
290
            $stateForm->title = BackendMaintenance::t('app', $stateForm->getDefaultTitle());
291
        }
292
        if ($stateForm->text === null && $this->content === null) {
293
            $stateForm->text = BackendMaintenance::t('app', $stateForm->getDefaultText());
294
        }
295
    }
296
297
    /**
298
     * Example format date time
299
     * @return mixed
300
     */
301
    protected function exampleDateFormat()
302
    {
303
        return date($this->state->getDateFormat());
304
    }
305
}
306
307