Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

HealthController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 94
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F actionIndex() 0 50 13
A actionMailer() 0 10 3
1
<?php
2
3
namespace luya\console\commands;
4
5
use Yii;
6
use yii\helpers\FileHelper;
7
8
/**
9
 * Health/Status informations about the Application itself.
10
 *
11
 * @author Basil Suter <[email protected]>
12
 * @since 1.0.0
13
 */
14
class HealthController extends \luya\console\Command
15
{
16
    /**
17
     * @var array An array with all folders to check where the value for the key is whether it should be writeable or not.
18
     */
19
    public $folders = [
20
        'public_html/assets' => true,
21
        'public_html/storage' => true,
22
        'migrations' => false,
23
        'vendor' => false,
24
        'runtime' => true,
25
    ];
26
27
    /**
28
     * @var array An array with files to check if they exists or not.
29
     */
30
    public $files = [
31
        'configs/env.php',
32
        'public_html/index.php',
33
    ];
34
    
35
    /**
36
     * Create all required directories an check whether they are writeable or not.
37
     *
38
     * @return string The action output.
39
     */
40
    public function actionIndex()
41
    {
42
        // opcache reset should run in web context, but might be helpfull as well.
43
        if (function_exists('opcache_reset')) {
44
            @opcache_reset();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
        }
46
47
        $error = false;
48
49
        @chdir(Yii::getAlias('@app'));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
50
51
        $this->output('The directory the health commands is applying to: ' . Yii::getAlias('@app'));
52
        
53
        foreach ($this->folders as $folder => $writable) {
54
            $mode = ($writable) ? 0777 : 0775;
55
            if (!file_exists($folder)) {
56
                if (FileHelper::createDirectory($folder, $mode)) {
57
                    $this->outputSuccess("$folder: successfully created directory");
58
                } else {
59
                    $error = true;
60
                    $this->outputError("$folder: unable to create directory");
61
                }
62
            } else {
63
                $this->outputInfo("$folder: directory exists already");
64
            }
65
            
66
            if ($writable && !is_writable($folder)) {
67
                $this->outputInfo("$folder: is not writeable, try to set mode '$mode'.");
68
                @chmod($folder, $mode);
69
            }
70
71
            if ($writable) {
72
                if (!is_writable($folder)) {
73
                    $error = true;
74
                    $this->outputError("$folder: is not writable, please change permissions.");
75
                }
76
            }
77
        }
78
79
        foreach ($this->files as $file) {
80
            if (file_exists($file)) {
81
                $this->outputInfo("$file: file exists.");
82
            } else {
83
                $error = true;
84
                $this->outputError("$file: file does not exists!");
85
            }
86
        }
87
        
88
        return $error ? $this->outputError('Health check found errors!') : $this->outputSuccess('O.K.');
89
    }
90
91
    /**
92
     * Test Mail-Component (Use --verbose=1 to enable smtp debug output)
93
     *
94
     * @return boolean Whether successfull or not.
95
     * @throws \Exception On smtp failure
96
     */
97
    public function actionMailer()
98
    {
99
        try {
100
            if (Yii::$app->mail->smtpTest($this->verbose)) {
101
                return $this->outputSuccess("successfull connected to SMTP Server '".Yii::$app->mail->host."'");
102
            }
103
        } catch (\Exception $e) {
104
            return $this->outputError($e->getMessage());
105
        }
106
    }
107
}
108