MaintenanceController   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

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