Completed
Push — master ( ff2b0b...f369f8 )
by Alexey
05:58
created

MaintenanceController::actionDisable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 15
rs 9.9666
cc 4
nc 3
nop 0
1
<?php
2
3
namespace common\components\maintenance\commands;
4
5
use yii\helpers\Console;
6
use yii\console\Controller;
7
use yii\base\Module;
8
use common\components\maintenance\interfaces\StateInterface;
9
use common\components\maintenance\states\FileState;
10
11
/**
12
 * Maintenance mode
13
 * @package common\components\maintenance\commands
14
 */
15
class MaintenanceController extends Controller
16
{
17
    /**
18
     * Date
19
     * @var string
20
     */
21
    public $date;
22
    /**
23
     * Title
24
     * @var string
25
     */
26
    public $title;
27
    /**
28
     * Content
29
     * @var string
30
     */
31
    public $content;
32
    /**
33
     * @var string
34
     */
35
    public $subscribe;
36
37
    /**
38
     * @var StateInterface
39
     */
40
    protected $state;
41
    /**
42
     * @var mixed string
43
     */
44
    protected $exampleData;
45
46
    /**
47
     * MaintenanceController constructor.
48
     * @param string $id
49
     * @param Module $module
50
     * @param StateInterface $state
51
     * @param array $config
52
     */
53
    public function __construct($id, Module $module, StateInterface $state, array $config = [])
54
    {
55
        $this->state = $state;
56
        $this->exampleData = $this->exampleDateFormat();
57
        parent::__construct($id, $module, $config);
58
    }
59
60
    /**
61
     * Options
62
     * @param string $actionID
63
     * @return array|string[]
64
     */
65
    public function options($actionID)
66
    {
67
        return [
68
            'date',
69
            'title',
70
            'content',
71
            'subscribe',
72
        ];
73
    }
74
75
    /**
76
     * Aliases
77
     * @return array
78
     */
79
    public function optionAliases()
80
    {
81
        return [
82
            'd' => 'date',
83
            't' => 'title',
84
            'c' => 'content',
85
            's' => 'subscribe'
86
        ];
87
    }
88
89
    /**
90
     * Maintenance status or commands
91
     */
92
    public function actionIndex()
93
    {
94
        if ($this->state->isEnabled()) {
95
            $enabled = $this->ansiFormat('ENABLED', Console::FG_RED);
96
            $datetime = $this->state->datetime();
0 ignored issues
show
Bug introduced by
The method datetime() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            /** @scrutinizer ignore-call */ 
97
            $datetime = $this->state->datetime();
Loading history...
97
            $this->stdout("Maintenance Mode has been $enabled\n");
98
            $this->stdout("on until $datetime\n");
99
            $this->stdout('Total (' . count($this->state->emails()) . ') followers.' . PHP_EOL);
0 ignored issues
show
Bug introduced by
The method emails() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
            $this->stdout('Total (' . count($this->state->/** @scrutinizer ignore-call */ emails()) . ') followers.' . PHP_EOL);
Loading history...
100
101
            $this->stdout("\nMaintenance Mode update date and time.\n");
102
            $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
103
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
104
105
            $this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n");
106
            $this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n");
107
108
            $this->stdout("\nMaintenance Mode disable.\n");
109
            $this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n");
110
        } else {
111
            $disabled = $this->ansiFormat('DISABLED', Console::FG_GREEN);
112
            $this->stdout("Maintenance Mode has been $disabled!\n");
113
114
            $this->stdout("\nMaintenance Mode enable.\n");
115
            $this->stdout("Use:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
116
117
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
118
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
119
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
120
121
            $this->stdout("\nMaintenance Mode update date and time.\n");
122
            $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
123
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
124
125
            $this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n");
126
            $this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n");
127
128
            $this->stdout("\nMaintenance Mode disable.\n");
129
            $this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n");
130
        }
131
    }
132
133
    /**
134
     * Enable maintenance mode
135
     */
136
    public function actionEnable()
137
    {
138
        $datetime = $this->date;
139
        if (!$this->state->isEnabled()) {
140
            $this->state->enable($datetime, $this->title, $this->content, $this->subscribe);
141
        }
142
        $datetime = $this->state->datetime();
143
        $enabled = $this->ansiFormat('ENABLED', Console::FG_RED);
144
        $this->stdout("Maintenance Mode has been $enabled\n");
145
        $this->stdout("on until $datetime\n");
146
147
        $this->stdout("\nMaintenance Mode update date and time.\n");
148
        $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
149
        $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
150
151
        $this->stdout("\nSubscribers to whom messages will be sent after turning off the mode maintenance\n");
152
        $this->stdout("Use:\nphp yii maintenance/followers\nto show followers.\n");
153
154
        $this->stdout("\nMaintenance Mode disable.\n");
155
        $this->stdout("Use:\nphp yii maintenance/disable\nto disable maintenance mode.\n");
156
    }
157
158
    /**
159
     * Update date and time maintenance mode
160
     */
161
    public function actionUpdate()
162
    {
163
        if ($this->state->isEnabled()) {
164
            $status = false;
165
            $updated = $this->ansiFormat('UPDATED', Console::FG_GREEN);
166
            if ($this->date) {
167
                if ($this->state->validDate($this->date)) {
0 ignored issues
show
Bug introduced by
The method validDate() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
                if ($this->state->/** @scrutinizer ignore-call */ validDate($this->date)) {
Loading history...
168
                    $this->state->update(FileState::MAINTENANCE_PARAM_DATE, $this->date);
169
                    $param = $this->state->datetime();
170
                    $this->stdout("Maintenance Mode has been $updated!\n");
171
                    $this->stdout("To: $param\n");
172
                    $status = true;
173
                } else {
174
                    $this->stdout("Invalid date and time format\n");
175
                    $this->stdout("Use:\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
176
                    $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
177
                    $status = true;
178
                }
179
            }
180
181
            if ($this->title) {
182
                $this->state->update(FileState::MAINTENANCE_PARAM_TITLE, $this->title);
183
                $param = $this->state->getParams(FileState::MAINTENANCE_PARAM_TITLE);
0 ignored issues
show
Bug introduced by
The method getParams() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
                /** @scrutinizer ignore-call */ 
184
                $param = $this->state->getParams(FileState::MAINTENANCE_PARAM_TITLE);
Loading history...
184
                $this->stdout("Maintenance Mode has been $updated!\n");
185
                $this->stdout("To: $param\n");
186
                $status = true;
187
            }
188
189
            if ($this->content) {
190
                $this->state->update(FileState::MAINTENANCE_PARAM_CONTENT, $this->content);
191
                $param = $this->state->getParams(FileState::MAINTENANCE_PARAM_CONTENT);
192
                $this->stdout("Maintenance Mode has been $updated!\n");
193
                $this->stdout("To: $param\n");
194
                $status = true;
195
            }
196
197
            if ($this->subscribe) {
198
                $this->state->update(FileState::MAINTENANCE_PARAM_SUBSCRIBE, $this->subscribe);
199
                $param = $this->state->getParams(FileState::MAINTENANCE_PARAM_SUBSCRIBE);
200
                $this->stdout("Maintenance Mode has been $updated!\n");
201
                $this->stdout("To: $param\n");
202
                $status = true;
203
            }
204
205
            if ($status === false) {
206
                $this->stdout("Not specified what to update\n");
207
                $this->stdout("\nUse:\n");
208
                $this->stdout("\nphp yii maintenance/update --date=\"$this->exampleData\"\nto update maintenance mode to $this->exampleData.\n");
209
                $this->stdout("\nphp yii maintenance/update --title=\"Maintenance\"\nto update maintenance mode title.\n");
210
                $this->stdout("\nphp yii maintenance/update --content=\"Maintenance\"\nto update maintenance mode text content.\n");
211
                $this->stdout("\nphp yii maintenance/update --subscribe=true\nto enable subscribe form for maintenance mode.\n");
212
            }
213
        } else {
214
            $this->stdout("Maintenance Mode not enable!\n");
215
216
            $this->stdout("Use:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
217
218
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
219
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
220
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
221
        }
222
    }
223
224
    /**
225
     * Disable maintenance mode
226
     */
227
    public function actionDisable()
228
    {
229
        $this->stdout("Maintenance Mode has been disabled.\n");
230
        if ($this->state->isEnabled()) {
231
            $result = $this->state->disable();
232
            if ($result || $result === 0) {
233
                $this->stdout("Notified ($result) subscribers.\n");
234
            }
235
        }
236
237
        $this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
238
239
        $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
240
        $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
241
        $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
242
    }
243
244
    /**
245
     * Show subscribers to whom messages
246
     */
247
    public function actionFollowers()
248
    {
249
        if (!$this->state->isEnabled()) {
250
            $this->stdout("Maintenance Mode not enable!\n");
251
252
            $this->stdout("\nUse:\nphp yii maintenance/enable\nto enable maintenance mode.\n");
253
254
            $this->stdout("\nAlso maintenance Mode enable set to date and time.\n");
255
            $this->stdout("Use:\nphp yii maintenance/enable --date=\"$this->exampleData\"\nto enable maintenance mode to $this->exampleData.\n");
256
            $this->stdout("Note:\nThis date and time not disable maintenance mode\n");
257
        } else if ($emails = $this->state->emails()) {
258
            $this->stdout('Total (' . count($emails) . ') followers:' . PHP_EOL);
259
            foreach ($emails as $email) {
260
                $this->stdout($email . PHP_EOL);
261
            }
262
        } else {
263
            $this->stdout("No followers\n");
264
        }
265
    }
266
267
    /**
268
     * Example format date time
269
     * @return mixed
270
     */
271
    protected function exampleDateFormat()
272
    {
273
        return $this->state->datetime(time());
274
    }
275
}
276
277