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 |
||
| 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) |
|
| 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) |
|
| 86 | |||
| 87 | 1 | public function setIps($value) |
|
| 91 | |||
| 92 | 1 | public function getIps() |
|
| 93 | { |
||
| 94 | 1 | if (empty($this->_ips)) { |
|
| 100 | |||
| 101 | 1 | public function findIps() |
|
| 110 | |||
| 111 | 1 | public function setLocalIps($value) |
|
| 115 | |||
| 116 | 1 | public function getLocalIps() |
|
| 124 | |||
| 125 | 1 | public function setWebDir($value) |
|
| 129 | |||
| 130 | 1 | public function getWebDir() |
|
| 138 | |||
| 139 | 1 | public function setLogDir($value) |
|
| 143 | |||
| 144 | 1 | public function getLogDir() |
|
| 152 | |||
| 153 | 1 | public function setFpmSocket($value) |
|
| 157 | |||
| 158 | 1 | public function getFpmSocket() |
|
| 166 | |||
| 167 | 1 | public function setSslDir($value) |
|
| 174 | |||
| 175 | 1 | public function getSslDir() |
|
| 183 | |||
| 184 | public function setAliases($aliases) |
||
| 191 | |||
| 192 | 1 | public function getAliases() |
|
| 196 | |||
| 197 | 1 | public function getDomains() |
|
| 204 | |||
| 205 | 1 | public function getServerName() |
|
| 209 | |||
| 210 | 1 | public function getAdditionalConfig() |
|
| 214 | |||
| 215 | public function setAdditionalConfig($additinalConfig) |
||
| 219 | } |
||
| 220 |