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

MaintenanceController   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 31
eloc 111
c 5
b 1
f 0
dl 0
loc 252
rs 9.92

11 Methods

Rating   Name   Duplication   Size   Complexity  
A actionDisable() 0 16 4
A actionUpdate() 0 40 3
A setFileStateForm() 0 18 6
A options() 0 8 1
A actionIndex() 0 6 2
A actionSubscribers() 0 28 4
A setDefaultValue() 0 7 5
A __construct() 0 4 1
A optionAliases() 0 8 1
A actionEnable() 0 12 3
A actionOptions() 0 3 1
1
<?php
2
3
namespace dominus77\maintenance\commands;
4
5
use yii\base\Module;
6
use dominus77\maintenance\base\ConsoleController;
7
use dominus77\maintenance\Maintenance;
8
use dominus77\maintenance\models\FileStateForm;
9
use dominus77\maintenance\interfaces\StateInterface;
10
11
/**
12
 * Maintenance mode
13
 * @package dominus77\maintenance\commands
14
 *
15
 * @property FileStateForm $defaultValue
16
 * @property FileStateForm $fileStateForm
17
 */
18
class MaintenanceController extends ConsoleController
19
{
20
    /**
21
     * Date
22
     * @var string
23
     */
24
    public $date;
25
    /**
26
     * Title
27
     * @var string
28
     */
29
    public $title;
30
    /**
31
     * Content
32
     * @var string
33
     */
34
    public $content;
35
    /**
36
     * @var string
37
     */
38
    public $subscribe;
39
40
    /**
41
     * @var string
42
     */
43
    public $timer;
44
45
    /**
46
     * @var StateInterface
47
     */
48
    protected $state;
49
50
    /**
51
     * MaintenanceController constructor.
52
     *
53
     * @param string $id
54
     * @param Module $module
55
     * @param StateInterface $state
56
     * @param array $config
57
     */
58
    public function __construct($id, Module $module, StateInterface $state, array $config = [])
59
    {
60
        $this->state = $state;
61
        parent::__construct($id, $module, $config);
62
    }
63
64
65
    /**
66
     * Options
67
     *
68
     * @param string $actionId
69
     * @return array|string[]
70
     */
71
    public function options($actionId)
72
    {
73
        return [
74
            'date',
75
            'title',
76
            'content',
77
            'subscribe',
78
            'timer'
79
        ];
80
    }
81
82
    /**
83
     * Aliases
84
     *
85
     * @return array
86
     */
87
    public function optionAliases()
88
    {
89
        return [
90
            'd' => 'date',
91
            't' => 'title',
92
            'c' => 'content',
93
            's' => 'subscribe',
94
            'tm' => 'timer'
95
        ];
96
    }
97
98
    /**
99
     * Maintenance status
100
     */
101
    public function actionIndex()
102
    {
103
        if ($this->stateForm->isEnabled()) {
104
            $this->renderGroupEnabled();
105
        } else {
106
            $this->renderGroupDisabled();
107
        }
108
    }
109
110
    /**
111
     * Enable maintenance mode
112
     */
113
    public function actionEnable()
114
    {
115
        $stateForm = new FileStateForm();
116
        if (!$this->stateForm->isEnabled()) {
117
            $stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE;
118
            $stateForm = $this->setFileStateForm($stateForm);
119
            $this->setDefaultValue($stateForm);
120
            if ($stateForm->validate()) {
121
                $stateForm->save();
122
            }
123
        }
124
        $this->renderGroupEnabled();
125
    }
126
127
    /**
128
     * Disable maintenance mode and send notify
129
     */
130
    public function actionDisable()
131
    {
132
        $status = $this->disabledMode;
133
        $this->renderMaintenanceModeHasBeenStatus($status);
134
        $this->stdout(PHP_EOL);
135
        if ($this->stateForm->isEnabled()) {
136
            $this->stateForm->disable();
137
            $result = $this->subscribeForm->send();
138
            if ($result || $result === 0) {
139
                $this->renderNotifiedSubscribers($result);
140
                $this->stdout(PHP_EOL);
141
            }
142
        }
143
        $this->renderEnableMaintenanceMode();
144
        $this->stdout(PHP_EOL);
145
        $this->renderOptionsTable();
146
    }
147
148
    /**
149
     * Update date and time maintenance mode
150
     */
151
    public function actionUpdate()
152
    {
153
        if ($this->stateForm->isEnabled()) {
154
            $this->stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE;
155
            $stateForm = $this->setFileStateForm($this->stateForm);
156
157
            if ($stateForm->validate()) {
158
                $stateForm->save();
159
160
                $status = $this->updatedMode;
161
                $this->renderMaintenanceModeHasBeenStatus($status);
162
                $this->stdout(PHP_EOL);
163
164
                $this->renderOnUntilDateTime();
165
                $this->stdout(PHP_EOL);
166
                $this->stdout(PHP_EOL);
167
168
                $this->renderCountDownStatus();
169
                $this->stdout(PHP_EOL);
170
                $this->renderSubscriptionFormStatus();
171
                $this->stdout(PHP_EOL);
172
173
                $this->renderUpdateMaintenanceMode();
174
                $this->stdout(PHP_EOL);
175
                $this->stdout(PHP_EOL);
176
177
                $this->renderOptionsTable();
178
            } else {
179
                $status = $this->notUpdatedMode;
180
                $this->renderMaintenanceModeHasBeenStatus($status);
181
                $this->stdout(PHP_EOL);
182
183
                $this->renderUpdateMaintenanceMode();
184
                $this->stdout(PHP_EOL);
185
                $this->stdout(PHP_EOL);
186
187
                $this->renderOptionsTable();
188
            }
189
        } else {
190
            $this->renderGroupDisabled();
191
        }
192
    }
193
194
    /**
195
     * Show subscribers to whom messages
196
     */
197
    public function actionSubscribers()
198
    {
199
        if (!$this->stateForm->isEnabled()) {
200
            $this->renderGroupDisabled();
201
        } else if ($emails = $this->subscribeForm->getEmails()) {
202
            $status = $this->enabledMode;
203
            $this->renderMaintenanceModeHasBeenStatus($status);
204
            $this->stdout(PHP_EOL);
205
206
            $this->renderOnUntilDateTime();
207
            $this->stdout(PHP_EOL);
208
209
            $this->renderSubscriptionInfo();
210
            $this->stdout(PHP_EOL);
211
            $this->stdout(PHP_EOL);
212
213
            foreach ($emails as $email) {
214
                $this->stdout($email . PHP_EOL);
215
            }
216
        } else {
217
            $status = $this->enabledMode;
218
            $this->renderMaintenanceModeHasBeenStatus($status);
219
            $this->stdout(PHP_EOL);
220
            $this->renderOnUntilDateTime();
221
            $this->stdout(PHP_EOL);
222
223
            $this->stdout(Maintenance::t('app', 'No subscribers'));
224
            $this->stdout(PHP_EOL);
225
        }
226
    }
227
228
    /**
229
     * Options
230
     */
231
    public function actionOptions()
232
    {
233
        $this->renderOptionsTable();
234
    }
235
236
    /**
237
     * @param FileStateForm $stateForm
238
     * @return FileStateForm
239
     */
240
    protected function setFileStateForm(FileStateForm $stateForm)
241
    {
242
        if ($this->date) {
243
            $stateForm->date = $this->date;
244
        }
245
        if ($this->title) {
246
            $stateForm->title = $this->title;
247
        }
248
        if ($this->content) {
249
            $stateForm->text = $this->content;
250
        }
251
        if ($this->subscribe) {
252
            $stateForm->subscribe = $this->subscribe === 'true';
253
        }
254
        if ($this->timer) {
255
            $stateForm->countDown = $this->timer === 'true';
256
        }
257
        return $stateForm;
258
    }
259
260
    /**
261
     * @param FileStateForm $stateForm
262
     */
263
    protected function setDefaultValue(FileStateForm $stateForm)
264
    {
265
        if ($stateForm->title === null && $this->title === null) {
266
            $stateForm->title = Maintenance::t('app', $stateForm->getDefaultTitle());
267
        }
268
        if ($stateForm->text === null && $this->content === null) {
269
            $stateForm->text = Maintenance::t('app', $stateForm->getDefaultText());
270
        }
271
    }
272
}
273
274