Completed
Push — master ( 18c273...6fc363 )
by Andrii
04:32 queued 56s
created

VhostController   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Test Coverage

Coverage 95.65%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 40
lcom 3
cbo 4
dl 0
loc 196
ccs 66
cts 69
cp 0.9565
rs 8.2608
c 2
b 1
f 1

25 Methods

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

How to fix   Complexity   

Complex Class

Complex classes like VhostController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 VhostController, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * HiDev Nginx plugin
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\controllers;
12
13
use hidev\modifiers\Sudo;
14
use Yii;
15
16
/**
17
 * Goal for Nginx virtual host.
18
 */
19
class VhostController extends \hidev\controllers\CommonController
20
{
21
    /**
22
     * @var NginxController
23
     */
24
    public $nginx;
25
26
    /**
27
     * @var integer
28
     */
29
    public $timeout;
30
31
    public $ssl;
32
33
    public $_aliases = [];
34
35
    protected $_sslDir;
36
    protected $_ips = [];
37
    protected $_localIps = [];
38
    protected $_domain;
39
    protected $_webDir;
40
    protected $_logDir;
41
    protected $_fpmSocket;
42
    protected $_additionalConfig;
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
52 1
    public function renderConf()
53
    {
54 1
        return $this->nginx->render('default.twig', [
55 1
            'this' => $this,
56
        ]);
57
    }
58
59 1
    public function setDomain($value)
60
    {
61 1
        $this->_domain = trim($value);
62 1
    }
63
64
    public function setDomains($domains)
65
    {
66
        if (!is_array($domains)) {
67
            $domains = preg_split('/[\s,]+/', trim($domains));
68
        }
69
        $this->_domain = array_shift($domains);
70
        $this->_aliases = $domains;
71
    }
72
73 1
    public function getDomain()
74
    {
75 1
        if ($this->_domain === null || $this->_domain === 'default') {
76 1
            $this->_domain = $this->takePackage()->name;
77
        }
78
79 1
        return $this->_domain;
80
    }
81
82 1
    public function setIp($value)
83
    {
84 1
        $this->_ips = [$value];
85 1
    }
86
87 1
    public function setIps($value)
88
    {
89 1
        $this->_ips = is_array($value) ? array_unique($value) : [$value];
90 1
    }
91
92 1
    public function getIps()
93
    {
94 1
        if (empty($this->_ips)) {
95 1
            $this->_ips = $this->findIps();
96
        }
97
98 1
        return $this->_ips;
99
    }
100
101 1
    public function findIps()
102
    {
103 1
        return [gethostbyname($this->getDomain()) ?: '127.0.0.1'];
104
    }
105
106 1
    public function setLocalIps($value)
107
    {
108 1
        $this->_localIps = is_array($value) ? array_unique($value) : [$value];
109 1
    }
110
111 1
    public function getLocalIps()
112
    {
113 1
        if (empty($this->_localIps)) {
114 1
            $this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps();
115
        }
116
117 1
        return $this->_localIps;
118
    }
119
120 1
    public function setWebDir($value)
121
    {
122 1
        $this->_webDir = Yii::getAlias($value);
123 1
    }
124
125 1
    public function getWebDir()
126
    {
127 1
        if ($this->_webDir === null) {
128
            $this->_webDir = $this->takeGoal('start')->buildRootPath('web');
129
        }
130
131 1
        return $this->_webDir;
132
    }
133
134 1
    public function setLogDir($value)
135
    {
136 1
        $this->_logDir = Yii::getAlias($value);
137 1
    }
138
139 1
    public function getLogDir()
140
    {
141 1
        if ($this->_logDir === null) {
142 1
            $this->_logDir = $this->nginx->getLogDir();
143
        }
144
145 1
        return $this->_logDir;
146
    }
147
148 1
    public function setFpmSocket($value)
149
    {
150 1
        $this->_fpmSocket = $value;
151 1
    }
152
153 1
    public function getFpmSocket()
154
    {
155 1
        if ($this->_fpmSocket === null) {
156 1
            $this->_fpmSocket = $this->nginx->getFpmSocket();
157
        }
158
159 1
        return $this->_fpmSocket;
160
    }
161
162 1
    public function setSslDir($value)
163
    {
164 1
        $this->_sslDir = Yii::getAlias($value);
165 1
        if ($this->_sslDir[0] !== '/') {
166
            $this->_sslDir = $this->takeGoal('start')->buildRootPath($this->_sslDir);
167
        }
168 1
    }
169
170 1
    public function getSslDir()
171
    {
172 1
        if ($this->_sslDir === null) {
173
            $this->_sslDir = $this->takeGoal('start')->buildRootPath('ssl');
174
        }
175
176 1
        return $this->_sslDir;
177
    }
178
179
    public function setAliases($aliases)
180
    {
181
        if (!is_array($aliases)) {
182
            $aliases = preg_split('/[\s,]+/', trim($aliases));
183
        }
184
        $this->_aliases = $aliases;
185
    }
186
187 1
    public function getAliases()
188
    {
189 1
        return $this->_aliases;
190
    }
191
192 1
    public function getDomains()
193
    {
194 1
        $domains = $this->getAliases();
195 1
        array_unshift($domains, $this->getDomain());
196
197 1
        return $domains;
198
    }
199
200 1
    public function getServerName()
201
    {
202 1
        return implode(' ', $this->getDomains());
203
    }
204
205 1
    public function getAdditionalConfig()
206
    {
207 1
        return $this->_additionalConfig;
208
    }
209
210
    public function setAdditionalConfig($additinalConfig)
211
    {
212
        $this->_additionalConfig = $additinalConfig;
213
    }
214
}
215