Completed
Push — master ( ea31e5...cf0b1d )
by Andrii
02:13
created

NginxController::actionDoLetsencrypt()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.2
cc 4
eloc 13
nc 5
nop 0
crap 20
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 Exception;
15
use hidev\base\File;
16
use hidev\modifiers\Sudo;
17
use Yii;
18
19
/**
20
 * Goal for Nginx.
21
 */
22
class NginxController extends \hidev\controllers\CommonController
23
{
24
    use \hiqdev\yii2\collection\ManagerTrait;
25
26
    protected $_logDir;
27
    protected $_etcDir;
28
    protected $_fpmSocket;
29
30
    public $defaultClass = VhostController::class;
31
32
    public function actionDump()
33
    {
34
        foreach ($this->getItems() as $vhost) {
35
            $conf = $vhost->renderConf();
36
            file_put_contents($vhost->getDomain() . '.conf', $conf);
37
        }
38
    }
39
40
    public function actionDeploy($aliases = [])
0 ignored issues
show
Unused Code introduced by
The parameter $aliases is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        return $this->perform('do-deploy');
0 ignored issues
show
Unused Code introduced by
The call to NginxController::perform() has too many arguments starting with 'do-deploy'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
    }
44
45
    public function actionDoDeploy()
46
    {
47
        $etcDir = $this->getEtcDir();
48
        if (!is_dir($etcDir)) {
49
            throw new InvalidParamException("Non existing Nginx etcDir: $etcDir");
50
        }
51
        $enabledDir   = $etcDir . DIRECTORY_SEPARATOR . 'sites-enabled';
52
        $availableDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-available';
53
        static::mkdir($enabledDir);
54
        static::mkdir($availableDir);
55
        foreach ($this->getItems() as $vhost) {
56
            $conf = $vhost->renderConf();
57
            $name = $vhost->getDomain() . '.conf';
58
            $file = File::plain($availableDir . DIRECTORY_SEPARATOR . $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...
59
            $file->save($conf);
60
            $file->symlink($enabledDir . DIRECTORY_SEPARATOR . $name);
61
        }
62
        $this->actionRestart();
63
    }
64
65
    public function actionStart()
66
    {
67
        return $this->make('start');
68
    }
69
70
    public function actionStop()
71
    {
72
        return $this->make('stop');
73
    }
74
75
    public function actionReload()
76
    {
77
        return $this->make('reload');
78
    }
79
80
    public function actionRestart()
81
    {
82
        return $this->make('restart');
83
    }
84
85
    public function actionStatus()
86
    {
87
        return $this->make('status', false);
88
    }
89
90
    public function make($operation, $sudo = true)
91
    {
92
        $args = ['nginx', $operation];
93
        if ($sudo) {
94
            array_push($args, Sudo::create());
95
        }
96
97
        return $this->passthru('service', $args);
98
    }
99
100
    public function actionMake()
101
    {
102
        return $this->make($this->performName ?: 'status');
0 ignored issues
show
Documentation introduced by
The property performName does not exist on object<hidev\nginx\controllers\NginxController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
    }
104
105
    public function actionLetsencrypt()
106
    {
107
        return $this->perform('do-letsencrypt');
0 ignored issues
show
Unused Code introduced by
The call to NginxController::perform() has too many arguments starting with 'do-letsencrypt'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
    }
109
110
    public function actionDoLetsencrypt()
111
    {
112
        foreach ($this->getItems() as $vhost) {
113
            $domain = $vhost->getDomain();
114
            $sslDir = $vhost->getSslDir();
115
            $args = ['certonly', '-a', 'webroot', '--webroot-path=' . $vhost->getWebDir()];
116
            foreach (array_reverse($vhost->getDomains()) as $name) {
117
                array_push($args, '-d');
118
                array_push($args, $name);
119
            }
120
            if ($this->passthru('/opt/letsencrypt/letsencrypt-auto', $args)) {
121
                throw new Exception('failed letsencrypt');
122
            }
123
            static::mkdir($sslDir);
124
            $this->passthru('sh', ['-c', "cp /etc/letsencrypt/live/$domain/* $sslDir", Sudo::create()]);
125
            $vhost->actionChmodSsl();
126
        }
127
    }
128
129
    public function actionChmodSsl()
130
    {
131
        foreach ($this->getItems() as $vhost) {
132
            $vhost->actionChmodSsl();
133
        }
134
    }
135
136
    public static function mkdir($path)
137
    {
138
        if (file_exists($path)) {
139
            return true;
140
        }
141
142
        return mkdir($path, 0777, true);
143
    }
144
145
    /**
146
     * Prepares item config.
147
     */
148
    public function getItemConfig($name = null, array $config = [])
149
    {
150
        return array_merge([
151
            'domain' => $name,
152
            'nginx'  => $this,
153
            'class'  => $this->defaultClass,
154
        ], $config);
155
    }
156
157
    public function createItem($id, $config = [])
158
    {
159
        return Yii::createObject($this->getItemConfig($id, $config), [$id, Yii::$app]);
160
    }
161
162
    public function setLogDir($value)
163
    {
164
        $this->_logDir = $value;
165
    }
166
167
    public function getLogDir()
168
    {
169
        if ($this->_logDir === null) {
170
            $this->_logDir = '/var/log/nginx';
171
        }
172
173
        return $this->_logDir;
174
    }
175
176
    public function setEtcDir($value)
177
    {
178
        $this->_etcDir = $value;
179
    }
180
181
    public function getEtcDir()
182
    {
183
        if ($this->_etcDir === null) {
184
            $this->_etcDir = $this->findEtcDir();
185
        }
186
187
        return $this->_etcDir;
188
    }
189
190
    public function findEtcDir()
191
    {
192
        $dirs = ['/etc/nginx', '/usr/local/etc/nginx'];
193
        foreach ($dirs as $dir) {
194
            if (is_dir($dir)) {
195
                return $dir;
196
            }
197
        }
198
199
        return reset($dirs);
200
    }
201
202
    public function setFpmSocket($value)
203
    {
204
        $this->_fpmSocket = $value;
205
    }
206
207
    public function getFpmSocket()
208
    {
209
        if ($this->_fpmSocket === null) {
210
            $this->_fpmSocket = 'unix:' . $this->findFpmSocketFile();
211
        }
212
213
        return $this->_fpmSocket;
214
    }
215
216
    public function findFpmSocketFile()
217
    {
218
        $files = ['/run/php/php7.0-fpm.sock', '/var/run/php5-fpm.sock'];
219
        foreach ($files as $file) {
220
            if (file_exists($file)) {
221
                return $file;
222
            }
223
        }
224
225
        return reset($files);
226
    }
227
}
228