Vhost   B
last analyzed

Complexity

Total Complexity 48

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 79
c 2
b 1
f 1
dl 0
loc 223
rs 8.5599
ccs 0
cts 161
cp 0
wmc 48

27 Methods

Rating   Name   Duplication   Size   Complexity  
A getPhpLogDir() 0 11 3
A getLocalIps() 0 7 3
A getSslDir() 0 7 2
A setDomains() 0 7 2
A setLogDir() 0 3 1
A setDomain() 0 3 1
A findIps() 0 8 3
A getLogDir() 0 7 2
A setFpmSocket() 0 3 1
A getWebDir() 0 7 2
A chmodSSL() 0 6 1
A setWebDir() 0 3 1
A renderConf() 0 4 1
A getAliases() 0 3 1
A getFpmSocket() 0 7 2
A getDomain() 0 7 3
A setSslDir() 0 5 2
A setIps() 0 3 2
A getAdditionalConfig() 0 3 1
A setAliases() 0 6 2
A setAdditionalConfig() 0 3 1
A getServerName() 0 3 1
A setIp() 0 3 1
A getDomains() 0 6 1
A getIps() 0 7 2
A setPhpLogDir() 0 3 1
A setLocalIps() 0 7 5

How to fix   Complexity   

Complex Class

Complex classes like Vhost often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Vhost, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * HiDev plugin for NGINX
4
 *
5
 * @link      https://github.com/hiqdev/hidev-nginx
6
 * @package   hidev-nginx
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\nginx\components;
12
13
use hidev\modifiers\Sudo;
14
use Yii;
15
use yii\helpers\StringHelper;
16
17
/**
18
 * NGINX virtual host component.
19
 */
20
class Vhost extends \hidev\base\Component
21
{
22
    /**
23
     * @var Nginx
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 $_phpLogDir;
43
    protected $_fpmSocket;
44
    protected $_additionalConfig;
45
46
    public function chmodSSL()
47
    {
48
        $dir = $this->getSslDir();
49
        $this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]);
50
        $this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]);
51
        $this->passthru('chmod', ['-R', 'o-rwx',    $dir, Sudo::create()]);
52
    }
53
54
    public function renderConf()
55
    {
56
        return $this->nginx->render('default.twig', [
57
            'this' => $this,
58
        ]);
59
    }
60
61
    public function setDomain($value)
62
    {
63
        $this->_domain = trim($value);
64
    }
65
66
    public function setDomains($domains)
67
    {
68
        if (!is_array($domains)) {
69
            $domains = preg_split('/[\s,]+/', trim($domains));
70
        }
71
        $this->_domain = array_shift($domains);
0 ignored issues
show
Bug introduced by
It seems like $domains can also be of type false; however, parameter $array of array_shift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $this->_domain = array_shift(/** @scrutinizer ignore-type */ $domains);
Loading history...
72
        $this->_aliases = $domains;
73
    }
74
75
    public function getDomain()
76
    {
77
        if ($this->_domain === null || $this->_domain === 'default') {
78
            $this->_domain = $this->take('package')->name;
79
        }
80
81
        return $this->_domain;
82
    }
83
84
    public function setIp($value)
85
    {
86
        $this->_ips = [$value];
87
    }
88
89
    public function setIps($value)
90
    {
91
        $this->_ips = is_array($value) ? array_unique($value) : [$value];
92
    }
93
94
    public function getIps()
95
    {
96
        if (empty($this->_ips)) {
97
            $this->_ips = $this->findIps();
98
        }
99
100
        return $this->_ips;
101
    }
102
103
    public function findIps()
104
    {
105
        $ips = gethostbyname($this->getDomain());
106
        if ($ips === $this->getDomain()) {
107
            $ips = '';
108
        }
109
110
        return [$ips ?: '127.0.0.1'];
111
    }
112
113
    public function setLocalIps($value)
114
    {
115
        if (!empty($value)) {
116
            if (is_string($value) && strpos($value, ',')!==false) {
117
                $value = explode(',', $value);
118
            }
119
            $this->_localIps = is_array($value) ? array_unique($value) : [$value];
120
        }
121
    }
122
123
    public function getLocalIps()
124
    {
125
        if (empty($this->_localIps)) {
126
            $this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps();
127
        }
128
129
        return $this->_localIps;
130
    }
131
132
    public function setWebDir($value)
133
    {
134
        $this->_webDir = Yii::getAlias($value);
135
    }
136
137
    public function getWebDir()
138
    {
139
        if ($this->_webDir === null) {
140
            $this->_webDir = Yii::getAlias('@root/public');
141
        }
142
143
        return $this->_webDir;
144
    }
145
146
    public function setPhpLogDir($value)
147
    {
148
        $this->_phpLogDir = Yii::getAlias($value);
149
    }
150
151
    public function getPhpLogDir()
152
    {
153
        if ($this->_phpLogDir === null) {
154
            $dir = $this->getLogDir();
155
            if (StringHelper::endsWith($dir, 'nginx')) {
156
                $dir = substr($dir, 0, -5) . 'php';
157
            }
158
            $this->_phpLogDir = $dir;
159
        }
160
161
        return $this->_phpLogDir;
162
    }
163
164
    public function setLogDir($value)
165
    {
166
        $this->_logDir = Yii::getAlias($value);
167
    }
168
169
    public function getLogDir()
170
    {
171
        if ($this->_logDir === null) {
172
            $this->_logDir = $this->nginx->getLogDir();
173
        }
174
175
        return $this->_logDir;
176
    }
177
178
    public function setFpmSocket($value)
179
    {
180
        $this->_fpmSocket = $value;
181
    }
182
183
    public function getFpmSocket()
184
    {
185
        if ($this->_fpmSocket === null) {
186
            $this->_fpmSocket = $this->nginx->getFpmSocket();
187
        }
188
189
        return $this->_fpmSocket;
190
    }
191
192
    public function setSslDir($value)
193
    {
194
        $this->_sslDir = Yii::getAlias($value);
195
        if ($this->_sslDir[0] !== '/') {
196
            $this->_sslDir = Yii::getAlias('@root/' . $this->_sslDir);
197
        }
198
    }
199
200
    public function getSslDir()
201
    {
202
        if ($this->_sslDir === null) {
203
            $this->_sslDir = Yii::getAlias('@root/ssl');
204
        }
205
206
        return $this->_sslDir;
207
    }
208
209
    public function setAliases($aliases)
210
    {
211
        if (!is_array($aliases)) {
212
            $aliases = preg_split('/[\s,]+/', trim($aliases));
213
        }
214
        $this->_aliases = $aliases;
215
    }
216
217
    public function getAliases()
218
    {
219
        return $this->_aliases;
220
    }
221
222
    public function getDomains()
223
    {
224
        $domains = $this->getAliases();
225
        array_unshift($domains, $this->getDomain());
226
227
        return $domains;
228
    }
229
230
    public function getServerName()
231
    {
232
        return implode(' ', $this->getDomains());
233
    }
234
235
    public function getAdditionalConfig()
236
    {
237
        return $this->_additionalConfig;
238
    }
239
240
    public function setAdditionalConfig($additinalConfig)
241
    {
242
        $this->_additionalConfig = $additinalConfig;
243
    }
244
}
245