| Total Complexity | 45 | 
| Total Lines | 343 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like Iptables 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 Iptables, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 36 | class Iptables extends BaseController  | 
            ||
| 37 | { | 
            ||
| 38 | /**  | 
            ||
| 39 | * Constructor  | 
            ||
| 40 | */  | 
            ||
| 41 | public function __construct()  | 
            ||
| 44 | }  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * The IPv4 table.  | 
            ||
| 48 | *  | 
            ||
| 49 | * @return ResponseInterface  | 
            ||
| 50 | */  | 
            ||
| 51 | public function ip4(): ResponseInterface  | 
            ||
| 52 |     { | 
            ||
| 53 |         return $this->iptables('IPv4'); | 
            ||
| 54 | }  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * The IPv6 table.  | 
            ||
| 58 | *  | 
            ||
| 59 | * @return ResponseInterface  | 
            ||
| 60 | */  | 
            ||
| 61 | public function ip6(): ResponseInterface  | 
            ||
| 62 |     { | 
            ||
| 63 |         return $this->iptables('IPv6'); | 
            ||
| 64 | }  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * The IPv4 table.  | 
            ||
| 68 | *  | 
            ||
| 69 | * @return ResponseInterface  | 
            ||
| 70 | */  | 
            ||
| 71 | public function ip4status(): ResponseInterface  | 
            ||
| 74 | }  | 
            ||
| 75 | |||
| 76 | /**  | 
            ||
| 77 | * The IPv6 table.  | 
            ||
| 78 | *  | 
            ||
| 79 | * @return ResponseInterface  | 
            ||
| 80 | */  | 
            ||
| 81 | public function ip6status(): ResponseInterface  | 
            ||
| 84 | }  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * System layer firwall - iptables  | 
            ||
| 88 | *  | 
            ||
| 89 | * @param string $type The type of IP address.  | 
            ||
| 90 | *  | 
            ||
| 91 | * @return void  | 
            ||
| 92 | */  | 
            ||
| 93 | protected function iptables(string $type = 'IPv4'): ResponseInterface  | 
            ||
| 94 |     { | 
            ||
| 95 | $reflection = new ReflectionObject($this->kernel);  | 
            ||
| 96 |         $t = $reflection->getProperty('properties'); | 
            ||
| 97 | $t->setAccessible(true);  | 
            ||
| 98 | $properties = $t->getValue($this->kernel);  | 
            ||
| 99 | |||
| 100 | $dir = $properties['iptables_watching_folder'];  | 
            ||
| 101 | |||
| 102 | $commandLogFile = $dir . '/' . strtolower($type) . '_command.log';  | 
            ||
| 103 | $iptablesQueueFile = $dir . '/iptables_queue.log';  | 
            ||
| 104 | |||
| 105 |         if ('POST' === get_request()->getMethod()) { | 
            ||
| 106 | $this->iptablesFormPost($type, $commandLogFile, $iptablesQueueFile);  | 
            ||
| 107 | }  | 
            ||
| 108 | |||
| 109 | $data = [];  | 
            ||
| 110 | $ipCommand = '';  | 
            ||
| 111 | |||
| 112 |         if (file_exists($commandLogFile)) { | 
            ||
| 113 | $file = new SplFileObject($commandLogFile);  | 
            ||
| 114 | |||
| 115 | $ipCommand = [];  | 
            ||
| 116 | |||
| 117 |             while (!$file->eof()) { | 
            ||
| 118 | $line = trim($file->fgets());  | 
            ||
| 119 |                 $ipInfo = explode(',', $line); | 
            ||
| 120 | |||
| 121 |                 if (!empty($ipInfo[4])) { | 
            ||
| 122 | $ipCommand[] = $ipInfo;  | 
            ||
| 123 | }  | 
            ||
| 124 | }  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | $data['ipCommand'] = $ipCommand;  | 
            ||
| 128 | $data['type'] = $type;  | 
            ||
| 129 | |||
| 130 |         $data['title'] = __('panel', 'title_iptables_manager', 'Iptables Manager') . ' (' . $type . ')'; | 
            ||
| 131 | |||
| 132 |         return $this->renderPage('panel/iptables_manager', $data); | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 133 | }  | 
            ||
| 134 | |||
| 135 | /**  | 
            ||
| 136 | * System layer firwall - iptables Status  | 
            ||
| 137 | * iptables -L  | 
            ||
| 138 | *  | 
            ||
| 139 | * @param string $type The type of IP address.  | 
            ||
| 140 | *  | 
            ||
| 141 | * @return ResponseInterface  | 
            ||
| 142 | */  | 
            ||
| 143 | protected function iptablesStatus(string $type = 'IPv4'): ResponseInterface  | 
            ||
| 144 |     { | 
            ||
| 145 | $data = [];  | 
            ||
| 146 | |||
| 147 | $reflection = new ReflectionObject($this->kernel);  | 
            ||
| 148 |         $t = $reflection->getProperty('properties'); | 
            ||
| 149 | $t->setAccessible(true);  | 
            ||
| 150 | $properties = $t->getValue($this->kernel);  | 
            ||
| 151 | |||
| 152 | $dir = $properties['iptables_watching_folder'];  | 
            ||
| 153 | |||
| 154 | // The iptables log files.  | 
            ||
| 155 | $ipStatusFile = $dir . '/ipv4_status.log';  | 
            ||
| 156 | |||
| 157 |         if ('IPv6' === $type) { | 
            ||
| 158 | $ipStatusFile = $dir . '/ipv6_status.log';  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | $ipStatus = '';  | 
            ||
| 162 | |||
| 163 |         if (file_exists($ipStatusFile)) { | 
            ||
| 164 | $ipStatus = file_get_contents($ipStatusFile);  | 
            ||
| 165 | }  | 
            ||
| 166 | |||
| 167 | $data['ipStatus'] = $ipStatus;  | 
            ||
| 168 | $data['type'] = $type;  | 
            ||
| 169 | |||
| 170 |         $data['title'] = __('panel', 'title_iptables_status', 'Iptables Status') . ' (' . $type . ')'; | 
            ||
| 171 | |||
| 172 |         return $this->renderPage('panel/iptables_status', $data); | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Detect and handle form post action.  | 
            ||
| 177 | *  | 
            ||
| 178 | * @param string $type IPv4 or IPv6  | 
            ||
| 179 | * @param string $commandLogFile The log file contains executed commands.  | 
            ||
| 180 | * @param string $iptablesQueueFile The file contains the commands that wil  | 
            ||
| 181 | * be executed by Iptables  | 
            ||
| 182 | *  | 
            ||
| 183 | * @return void  | 
            ||
| 184 | */  | 
            ||
| 185 | private function iptablesFormPost(string $type, string $commandLogFile, string $iptablesQueueFile)  | 
            ||
| 255 | }  | 
            ||
| 256 | }  | 
            ||
| 257 | }  | 
            ||
| 258 | |||
| 259 | /**  | 
            ||
| 260 | * Verify the form fields.  | 
            ||
| 261 | *  | 
            ||
| 262 | * @param array $postParams  | 
            ||
| 263 | *  | 
            ||
| 264 | * @return bool  | 
            ||
| 265 | */  | 
            ||
| 266 | private function iptablesFormPostVerification(array $postParams): bool  | 
            ||
| 267 |     { | 
            ||
| 268 |         if (!$this->checkFieldIp($postParams)) { | 
            ||
| 269 | return false;  | 
            ||
| 270 | }  | 
            ||
| 271 | |||
| 272 |         if (!$this->checkFieldPort($postParams)) { | 
            ||
| 273 | return false;  | 
            ||
| 274 | }  | 
            ||
| 275 | |||
| 276 |         if (!$this->checkFieldSubnet($postParams)) { | 
            ||
| 277 | return false;  | 
            ||
| 278 | }  | 
            ||
| 279 | |||
| 280 |         if (!$this->checkFieldProtocol($postParams)) { | 
            ||
| 281 | return false;  | 
            ||
| 282 | }  | 
            ||
| 283 | |||
| 284 |         if (!$this->checkFieldAction($postParams)) { | 
            ||
| 285 | return false;  | 
            ||
| 286 | }  | 
            ||
| 287 | |||
| 288 | return true;  | 
            ||
| 289 | }  | 
            ||
| 290 | |||
| 291 | /**  | 
            ||
| 292 | * Verify the form field - Ip.  | 
            ||
| 293 | *  | 
            ||
| 294 | * @param array $postParams  | 
            ||
| 295 | *  | 
            ||
| 296 | * @return bool  | 
            ||
| 297 | */  | 
            ||
| 298 | private function checkFieldIp($postParams): bool  | 
            ||
| 299 |     { | 
            ||
| 300 | if (  | 
            ||
| 301 | isset($postParams['ip']) &&  | 
            ||
| 302 |             filter_var(explode('/', $postParams['ip'])[0], FILTER_VALIDATE_IP) | 
            ||
| 303 |         ) { | 
            ||
| 304 | return true;  | 
            ||
| 305 | }  | 
            ||
| 306 | return false;  | 
            ||
| 307 | }  | 
            ||
| 308 | |||
| 309 | /**  | 
            ||
| 310 | * Verify the form field - Port.  | 
            ||
| 311 | *  | 
            ||
| 312 | * @param array $postParams  | 
            ||
| 313 | *  | 
            ||
| 314 | * @return bool  | 
            ||
| 315 | */  | 
            ||
| 316 | private function checkFieldPort($postParams): bool  | 
            ||
| 317 |     { | 
            ||
| 318 | if (  | 
            ||
| 319 | isset($postParams['port']) &&  | 
            ||
| 320 | (is_numeric($postParams['port']) || in_array($postParams['port'], ['all', 'custom']))  | 
            ||
| 321 |         ) { | 
            ||
| 322 | return true;  | 
            ||
| 323 | }  | 
            ||
| 324 | return false;  | 
            ||
| 325 | }  | 
            ||
| 326 | |||
| 327 | /**  | 
            ||
| 328 | * Verify the form field - Subnet.  | 
            ||
| 329 | *  | 
            ||
| 330 | * @param array $postParams  | 
            ||
| 331 | *  | 
            ||
| 332 | * @return bool  | 
            ||
| 333 | */  | 
            ||
| 334 | private function checkFieldSubnet($postParams): bool  | 
            ||
| 343 | }  | 
            ||
| 344 | |||
| 345 | /**  | 
            ||
| 346 | * Verify the form field - Protocol.  | 
            ||
| 347 | *  | 
            ||
| 348 | * @param array $postParams  | 
            ||
| 349 | *  | 
            ||
| 350 | * @return bool  | 
            ||
| 351 | */  | 
            ||
| 352 | private function checkFieldProtocol($postParams): bool  | 
            ||
| 353 |     { | 
            ||
| 354 | if (  | 
            ||
| 355 | isset($postParams['protocol']) &&  | 
            ||
| 356 | in_array($postParams['protocol'], ['tcp', 'udp', 'all'])  | 
            ||
| 357 |         ) { | 
            ||
| 358 | return true;  | 
            ||
| 359 | }  | 
            ||
| 360 | return false;  | 
            ||
| 361 | }  | 
            ||
| 362 | |||
| 363 | /**  | 
            ||
| 364 | * Verify the form field - Action.  | 
            ||
| 365 | *  | 
            ||
| 366 | * @param array $postParams  | 
            ||
| 367 | *  | 
            ||
| 368 | * @return bool  | 
            ||
| 369 | */  | 
            ||
| 370 | private function checkFieldAction($postParams): bool  | 
            ||
| 379 | }  | 
            ||
| 380 | }  | 
            ||
| 381 | |||
| 382 |