MaintenanceController::actionOn()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace app\commands;
4
5
use Yii;
6
use yii\console\{Controller, ExitCode};
7
use yii\helpers\Console;
8
9
/**
10
 * Command for enabling and disabling maintenance mode
11
 */
12
class MaintenanceController extends Controller
13
{
14 1
    public function actionOff(): int
15
    {
16 1
        $this->stdout("Trying disabling maintenance mode...\n");
17 1
        $file = $this->resolveFile();
18
19 1
        if (file_exists($file)) {
20 1
            unlink($file);
21 1
            $this->stdout("Done\n", Console::FG_GREEN);
22 1
            return ExitCode::OK;
23
        }
24 1
25 1
        $this->stdout("Application is NOT in maintenance mode.\n", Console::FG_YELLOW);
26
        return ExitCode::USAGE;
27
    }
28 1
29
    public function actionOn(): int
30 1
    {
31 1
        $this->stdout("Trying enabling maintenance mode...\n");
32
        $file = $this->resolveFile();
33 1
34 1
        if (!file_exists($file)) {
35 1
            file_put_contents($file, time());
36 1
            $this->stdout("Done\n", Console::FG_GREEN);
37
            return ExitCode::OK;
38 1
        }
39 1
40
        $this->stdout("Application is already in maintenance mode.\n", Console::FG_YELLOW);
41
        return ExitCode::USAGE;
42
    }
43
44
    /**
45
     * Returns the path to the 'maintenance' file
46
     *
47 2
     * @return string
48
     */
49 2
    private function resolveFile(): string
50
    {
51
        return Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'maintenance';
52
    }
53
}
54