Completed
Push — master ( c11925...0742b1 )
by Dmitry
08:14
created

VhostController::getWebDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 2
cts 4
cp 0.5
crap 2.5
rs 9.4285
c 0
b 0
f 0
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\modifiers\Sudo;
15
use Yii;
16
17
/**
18
 * Goal for Nginx virtual host.
19
 */
20
class VhostController extends \hidev\controllers\CommonController
21
{
22
    /**
23
     * @var NginxController
24
     */
25
    public $nginx;
26
27
    /**
28
     * @var integer
29
     */
30
    public $timeout;
31
32
    public $ssl;
33
34
    public $_aliases = [];
35
36
    protected $_sslDir;
37
    protected $_ips = [];
38
    protected $_localIps = [];
39
    protected $_domain;
40
    protected $_webDir;
41
    protected $_logDir;
42
    protected $_fpmSocket;
43
44
    public function actionChmodSsl()
45
    {
46
        $dir = $this->getSslDir();
47
        $this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]);
48
        $this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]);
49
        $this->passthru('chmod', ['-R', 'o-rwx',    $dir, Sudo::create()]);
50
    }
51 1
52
    public function renderConf()
53 1
    {
54 1
        return $this->nginx->render('default.twig', [
55 1
            'this' => $this,
56
        ]);
57
    }
58 1
59
    public function setDomain($value)
60 1
    {
61 1
        $this->_domain = $value;
62
    }
63 1
64
    public function getDomain()
65 1
    {
66 1
        if ($this->_domain === null || $this->_domain === 'default') {
67 1
            $this->_domain = $this->takePackage()->name;
68
        }
69 1
70
        return $this->_domain;
71
    }
72 1
73
    public function setIp($value)
74 1
    {
75 1
        $this->_ips = [$value];
76
    }
77 1
78
    public function setIps($value)
79 1
    {
80 1
        $this->_ips = is_array($value) ? array_unique($value) : [$value];
81
    }
82 1
83
    public function getIps()
84 1
    {
85 1
        if (empty($this->_ips)) {
86 1
            $this->_ips = $this->findIps();
87
        }
88 1
89
        return $this->_ips;
90
    }
91 1
92
    public function findIps()
93 1
    {
94
        return [gethostbyname($this->getDomain()) ?: '127.0.0.1'];
95
    }
96 1
97
    public function setLocalIps($value)
98 1
    {
99 1
        $this->_localIps = is_array($value) ? array_unique($value) : [$value];
100
    }
101 1
102
    public function getLocalIps()
103 1
    {
104 1
        if (empty($this->_localIps)) {
105 1
            $this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps();
106
        }
107 1
108
        return $this->_localIps;
109
    }
110 1
111
    public function setWebDir($value)
112 1
    {
113 1
        $this->_webDir = Yii::getAlias($value);
114
    }
115 1
116
    public function getWebDir()
117 1
    {
118
        if ($this->_webDir === null) {
119
            $this->_webDir = $this->takeGoal('start')->buildRootPath('web');
120
        }
121 1
122
        return $this->_webDir;
123
    }
124 1
125
    public function setLogDir($value)
126 1
    {
127 1
        $this->_logDir = Yii::getAlias($value);
128
    }
129 1
130
    public function getLogDir()
131 1
    {
132 1
        if ($this->_logDir === null) {
133 1
            $this->_logDir = $this->nginx->getLogDir();
134
        }
135 1
136
        return $this->_logDir;
137
    }
138 1
139
    public function setFpmSocket($value)
140 1
    {
141 1
        $this->_fpmSocket = $value;
142
    }
143 1
144
    public function getFpmSocket()
145 1
    {
146 1
        if ($this->_fpmSocket === null) {
147 1
            $this->_fpmSocket = $this->nginx->getFpmSocket();
148
        }
149 1
150
        return $this->_fpmSocket;
151
    }
152 1
153
    public function setSslDir($value)
154 1
    {
155 1
        $this->_sslDir = Yii::getAlias($value);
156
        if ($this->_sslDir[0] !== '/') {
157
            $this->_sslDir = $this->takeGoal('start')->buildRootPath($this->_sslDir);
158 1
        }
159
    }
160 1
161
    public function getSslDir()
162 1
    {
163
        if ($this->_sslDir === null) {
164
            $this->_sslDir = $this->takeGoal('start')->buildRootPath('ssl');
165
        }
166 1
167
        return $this->_sslDir;
168
    }
169
170
    public function setAliases($aliases)
171
    {
172
        if (!is_array($aliases)) {
173
            $aliases = preg_split('/[\s,]+/', trim($aliases));
174
        }
175
        $this->_aliases = $aliases;
176
    }
177 1
178
    public function getAliases()
179 1
    {
180
        return $this->_aliases;
181
    }
182 1
183
    public function getDomains()
184 1
    {
185 1
        $domains = $this->getAliases();
186
        array_unshift($domains, $this->getDomain());
187 1
188
        return $domains;
189
    }
190 1
191
    public function getServerName()
192 1
    {
193
        return implode(' ', $this->getDomains());
194
    }
195
}
196