Total Complexity | 45 |
Total Lines | 257 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Portal 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 Portal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Portal extends Command |
||
16 | { |
||
17 | use LogTrait; |
||
18 | |||
19 | protected $basePath; |
||
20 | |||
21 | /** |
||
22 | * @var InputInterface $input |
||
23 | */ |
||
24 | protected $input; |
||
25 | |||
26 | /** |
||
27 | * @var OutputInterface $output |
||
28 | */ |
||
29 | protected $output; |
||
30 | |||
31 | public function __construct($basePath) |
||
35 | } |
||
36 | |||
37 | protected function configure() |
||
46 | } |
||
47 | |||
48 | protected function execute(InputInterface $input, OutputInterface $output) |
||
102 | } |
||
103 | } |
||
104 | |||
105 | public function start() |
||
137 | } |
||
138 | |||
139 | public function stop() |
||
176 | } |
||
177 | } |
||
178 | |||
179 | public function restart() |
||
186 | } |
||
187 | |||
188 | public function reload() |
||
189 | { |
||
190 | $config = $this->getConfig(); |
||
191 | $pidFile = $config['server']['swoole']['pid_file']; |
||
192 | if (!file_exists($pidFile)) { |
||
193 | $this->error('It seems that Swoole is not running.'); |
||
194 | return; |
||
195 | } |
||
196 | |||
197 | // Reload worker process |
||
198 | $pid = file_get_contents($pidFile); |
||
199 | if (!$pid || !self::kill($pid, 0)) { |
||
200 | $this->error("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
201 | return; |
||
202 | } |
||
203 | |||
204 | if (self::kill($pid, SIGUSR1)) { |
||
205 | $now = date('Y-m-d H:i:s'); |
||
206 | $this->info("Swoole [PID={$pid}] is reloaded at {$now}."); |
||
207 | } else { |
||
208 | $this->error("Swoole [PID={$pid}] is reloaded failed."); |
||
209 | } |
||
210 | |||
211 | // Reload timer process |
||
212 | $pidFile = $config['server']['timer']['pid_file']; |
||
213 | $pid = file_get_contents($pidFile); |
||
214 | if (!$pid || !self::kill($pid, 0)) { |
||
215 | $this->error("Swoole Timer [PID={$pid}] does not exist, or permission denied."); |
||
216 | return; |
||
217 | } |
||
218 | |||
219 | if (self::kill($pid, SIGUSR1)) { |
||
220 | $now = date('Y-m-d H:i:s'); |
||
221 | $this->info("Swoole Timer [PID={$pid}] is reloaded at {$now}."); |
||
222 | } else { |
||
223 | $this->error("Swoole Timer [PID={$pid}] is reloaded failed."); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | public function showInfo() |
||
228 | { |
||
229 | $this->runArtisanCommand('laravels info'); |
||
230 | } |
||
231 | |||
232 | public function artisanCmd($subCmd) |
||
233 | { |
||
234 | $phpCmd = sprintf('%s -c "%s"', PHP_BINARY, php_ini_loaded_file()); |
||
235 | $env = $this->input->getOption('env'); |
||
236 | $envs = $env ? "APP_ENV={$env}" : ''; |
||
237 | $artisanCmd = trim(sprintf('%s %s %s/artisan %s', $envs, $phpCmd, $this->basePath, $subCmd)); |
||
238 | return $artisanCmd; |
||
239 | } |
||
240 | |||
241 | public function runArtisanCommand($cmd) |
||
242 | { |
||
243 | $cmd = $this->artisanCmd($cmd); |
||
244 | self::runCommand($cmd); |
||
245 | } |
||
246 | |||
247 | public function getConfig() |
||
248 | { |
||
249 | $json = file_get_contents($this->basePath . '/storage/laravels.json'); |
||
250 | return (array)json_decode($json, true); |
||
251 | } |
||
252 | |||
253 | public static function runCommand($cmd, $input = null) |
||
254 | { |
||
255 | $fp = popen($cmd, 'w'); |
||
256 | if ($fp === false) { |
||
257 | return false; |
||
258 | } |
||
259 | if ($input !== null) { |
||
260 | fwrite($fp, $input); |
||
261 | } |
||
262 | pclose($fp); |
||
263 | return true; |
||
264 | } |
||
265 | |||
266 | public static function kill($pid, $sig) |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 |