Complex classes like NginxController 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 NginxController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class NginxController extends \hidev\controllers\CommonController |
||
22 | { |
||
23 | use \hiqdev\yii2\collection\ManagerTrait; |
||
24 | |||
25 | protected $_logDir; |
||
26 | protected $_etcDir; |
||
27 | protected $_fpmSocket; |
||
28 | |||
29 | public $defaultClass = VhostController::class; |
||
30 | |||
31 | 1 | public function actionDoDump() |
|
32 | { |
||
33 | 1 | foreach ($this->getItems() as $vhost) { |
|
34 | 1 | $conf = $vhost->renderConf(); |
|
35 | 1 | file_put_contents($vhost->getDomain() . '.conf', $conf); |
|
36 | } |
||
37 | 1 | } |
|
38 | |||
39 | 1 | public function actionDump() |
|
43 | |||
44 | public function actionDeploy($aliases = []) |
||
48 | |||
49 | public function actionDoDeploy() |
||
50 | { |
||
51 | $etcDir = $this->getEtcDir(); |
||
52 | if (!is_dir($etcDir)) { |
||
53 | throw new InvalidParamException("Non existing Nginx etcDir: $etcDir"); |
||
54 | } |
||
55 | $enabledDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-enabled'; |
||
56 | $availableDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-available'; |
||
57 | static::mkdir($enabledDir); |
||
58 | static::mkdir($availableDir); |
||
59 | foreach ($this->getItems() as $vhost) { |
||
60 | $conf = $vhost->renderConf(); |
||
61 | $name = $vhost->getDomain() . '.conf'; |
||
62 | $file = File::plain($availableDir . DIRECTORY_SEPARATOR . $name); |
||
63 | $file->save($conf); |
||
64 | $file->symlink($enabledDir . DIRECTORY_SEPARATOR . $name); |
||
65 | } |
||
66 | $this->actionRestart(); |
||
67 | } |
||
68 | |||
69 | public function actionStart() |
||
73 | |||
74 | public function actionStop() |
||
78 | |||
79 | public function actionReload() |
||
83 | |||
84 | public function actionRestart() |
||
88 | |||
89 | public function actionStatus() |
||
93 | |||
94 | public function make($operation, $sudo = true) |
||
95 | { |
||
96 | $args = ['nginx', $operation]; |
||
97 | if ($sudo) { |
||
98 | array_push($args, Sudo::create()); |
||
99 | } |
||
100 | |||
101 | return $this->passthru('service', $args); |
||
102 | } |
||
103 | |||
104 | public function actionMake() |
||
108 | |||
109 | public function actionLetsencrypt() |
||
113 | |||
114 | public function actionDoLetsencrypt() |
||
115 | { |
||
116 | foreach ($this->getItems() as $vhost) { |
||
117 | $domain = $vhost->getDomain(); |
||
118 | $sslDir = $vhost->getSslDir(); |
||
119 | $args = ['certonly', '-a', 'webroot', '--webroot-path=' . $vhost->getWebDir()]; |
||
120 | foreach (array_reverse($vhost->getDomains()) as $name) { |
||
121 | array_push($args, '-d'); |
||
122 | array_push($args, $name); |
||
123 | } |
||
124 | if ($this->passthru('/opt/letsencrypt/letsencrypt-auto', $args)) { |
||
125 | throw new Exception('failed letsencrypt'); |
||
126 | } |
||
127 | static::mkdir($sslDir); |
||
128 | $this->passthru('sh', ['-c', "cp /etc/letsencrypt/live/$domain/* $sslDir", Sudo::create()]); |
||
129 | $vhost->actionChmodSsl(); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | public function actionChmodSsl() |
||
134 | { |
||
135 | foreach ($this->getItems() as $vhost) { |
||
136 | $vhost->actionChmodSsl(); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | public static function mkdir($path) |
||
148 | |||
149 | /** |
||
150 | * Prepares item config. |
||
151 | */ |
||
152 | 1 | public function getItemConfig($name = null, array $config = []) |
|
153 | { |
||
154 | 1 | return array_merge([ |
|
155 | 1 | 'domain' => $name, |
|
156 | 1 | 'nginx' => $this, |
|
157 | 1 | 'class' => $this->defaultClass, |
|
158 | ], $config); |
||
159 | } |
||
160 | |||
161 | 1 | public function createItem($id, $config = []) |
|
165 | |||
166 | public function setLogDir($value) |
||
170 | |||
171 | 1 | public function getLogDir() |
|
172 | { |
||
173 | 1 | if ($this->_logDir === null) { |
|
174 | 1 | $this->_logDir = '/var/log/nginx'; |
|
175 | } |
||
176 | |||
177 | 1 | return $this->_logDir; |
|
178 | } |
||
179 | |||
180 | public function setEtcDir($value) |
||
184 | |||
185 | public function getEtcDir() |
||
186 | { |
||
187 | if ($this->_etcDir === null) { |
||
188 | $this->_etcDir = $this->findEtcDir(); |
||
189 | } |
||
190 | |||
191 | return $this->_etcDir; |
||
192 | } |
||
193 | |||
194 | public function findEtcDir() |
||
195 | { |
||
196 | $dirs = ['/etc/nginx', '/usr/local/etc/nginx']; |
||
197 | foreach ($dirs as $dir) { |
||
198 | if (is_dir($dir)) { |
||
199 | return $dir; |
||
200 | } |
||
201 | } |
||
202 | |||
203 | return reset($dirs); |
||
204 | } |
||
205 | |||
206 | 1 | public function setFpmSocket($value) |
|
210 | |||
211 | 1 | public function getFpmSocket() |
|
212 | { |
||
213 | 1 | if ($this->_fpmSocket === null) { |
|
214 | $this->_fpmSocket = 'unix:' . $this->findFpmSocketFile(); |
||
215 | } |
||
216 | |||
217 | 1 | return $this->_fpmSocket; |
|
218 | } |
||
219 | |||
220 | public function findFpmSocketFile() |
||
231 | } |
||
232 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.