Completed
Push — master ( 1ce5d6...d981b7 )
by Andrii
02:02
created

NginxController::mkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * HiDev Nginx plugin
5
 *
6
 * @link      https://github.com/hiqdev/hidev-nginx
7
 * @package   hidev-nginx
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\nginx\controllers;
13
14
use hidev\base\File;
15
use hidev\modifiers\Sudo;
16
use Yii;
17
18
/**
19
 * Goal for Nginx.
20
 */
21
class NginxController extends \hidev\controllers\CommonController
22
{
23
    use \hiqdev\yii2\collection\ManagerTrait;
24
25
    protected $_logDir;
26
    protected $_etcDir;
27
    protected $_fpmSocket;
28
29
    public $defaultClass = VhostController::class;
30
31
    public function actionDump()
32
    {
33
        foreach ($this->getItems() as $vhost) {
34
            $conf = $vhost->renderConf();
35
            file_put_contents($vhost->getDomain() . '.conf', $conf);
36
        }
37
    }
38
39
    public function actionDeploy()
40
    {
41
        if (!file_exists($this->getEtcDir())) {
42
            throw new InvalidParamException('Non existing Nginx etcDir: ' . $this->getEtcDir());
43
        }
44
        $enabledDir   = $this->getEtcDir() . '/sites-enabled';
45
        $availableDir = $this->getEtcDir() . '/sites-available';
46
        $this->mkdir($enabledDir);
47
        $this->mkdir($availableDir);
48
        foreach ($this->getItems() as $vhost) {
49
            $conf = $vhost->renderConf();
50
            $name = $vhost->getDomain() . '.conf';
51
            $dest = $enabledDir . '/' . $name;
0 ignored issues
show
Unused Code introduced by
$dest is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
            $file = File::plain($availableDir . '/' . $name);
0 ignored issues
show
Bug introduced by
The method plain() does not seem to exist on object<hidev\base\File>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            $file->save($conf);
54
            $file->symlink($enabledDir . '/' . $name);
55
        }
56
        $this->actionRestart();
57
    }
58
59
    public function actionRestart()
60
    {
61
        return $this->passthru('service', ['nginx', 'restart', Sudo::create()]);
62
    }
63
64
    public static function mkdir($path)
65
    {
66
        if (file_exists($path)) {
67
            return true;
68
        }
69
70
        return mkdir($path, 0777, true);
71
    }
72
73
    /**
74
     * Prepares item config.
75
     */
76
    public function getItemConfig($name = null, array $config = [])
77
    {
78
        return array_merge([
79
            'domain' => $name,
80
            'nginx'  => $this,
81
            'class'  => $this->defaultClass,
82
        ], $config);
83
    }
84
85
    public function createItem($id, $config = [])
86
    {
87
        return Yii::createObject($this->getItemConfig($id, $config), [$id, Yii::$app]);
88
    }
89
90
    public function setLogDir($value)
91
    {
92
        $this->_logDir = $value;
93
    }
94
95
    public function getLogDir()
96
    {
97
        if ($this->_logDir === null) {
98
            $this->_logDir = '/var/log/nginx';
99
        }
100
101
        return $this->_logDir;
102
    }
103
104
    public function setEtcDir($value)
105
    {
106
        $this->_etcDir = $value;
107
    }
108
109
    public function getEtcDir()
110
    {
111
        if ($this->_etcDir === null) {
112
            $this->_etcDir = $this->findEtcDir();
113
        }
114
115
        return $this->_etcDir;
116
    }
117
118
    public function findEtcDir()
119
    {
120
        $dirs = ['/etc/nginx', '/usr/local/etc/nginx'];
121
        foreach ($dirs as $dir) {
122
            if (is_dir($dir)) {
123
                return $dir;
124
            }
125
        }
126
127
        return reset($dirs);
128
    }
129
130
    public function setFpmSocket($value)
131
    {
132
        $this->_fpmSocket = $value;
133
    }
134
135
    public function getFpmSocket()
136
    {
137
        if ($this->_fpmSocket === null) {
138
            $this->_fpmSocket = 'unix:' . $this->findFpmSocketFile();
139
        }
140
141
        return $this->_fpmSocket;
142
    }
143
144
    public function findFpmSocketFile()
145
    {
146
        $files = ['/run/php/php7.0-fpm.sock', '/var/run/php5-fpm.sock'];
147
        foreach ($files as $file) {
148
            if (file_exists($file)) {
149
                return $file;
150
            }
151
        }
152
153
        return reset($files);
154
    }
155
}
156