MaintenanceController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 42
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A actionOff() 0 14 2
A actionOn() 0 14 2
A resolveFile() 0 4 1
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