| Conditions | 11 |
| Paths | 36 |
| Total Lines | 60 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 61 | public function generateConf($not_ssl = false, $level = 0): void |
||
| 62 | { |
||
| 63 | $configPath = '/etc/nginx/mikopbx/conf.d'; |
||
| 64 | $httpConfigFile = "{$configPath}/http-server.conf"; |
||
| 65 | $httpsConfigFile = "{$configPath}/https-server.conf"; |
||
| 66 | $dns_server = '127.0.0.1'; |
||
| 67 | |||
| 68 | $net = new Network(); |
||
| 69 | $dns = $net->getHostDNS(); |
||
| 70 | foreach ($dns as $ns) { |
||
| 71 | if (Verify::isIpAddress($ns)) { |
||
| 72 | $dns_server = trim($ns); |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | // HTTP |
||
| 78 | $WEBPort = $this->mikoPBXConfig->getGeneralSettings('WEBPort'); |
||
| 79 | $WEBHTTPSPort = $this->mikoPBXConfig->getGeneralSettings('WEBHTTPSPort'); |
||
| 80 | |||
| 81 | $config = file_get_contents("{$httpConfigFile}.original"); |
||
| 82 | $config = str_replace(['<DNS>', '<WEBPort>'], [$dns_server, $WEBPort], $config); |
||
| 83 | |||
| 84 | $RedirectToHttps = $this->mikoPBXConfig->getGeneralSettings('RedirectToHttps'); |
||
| 85 | if ($RedirectToHttps === '1' && $not_ssl === false) { |
||
| 86 | $conf_data = 'if ( $remote_addr != "127.0.0.1" ) {' . PHP_EOL |
||
| 87 | . ' ' . 'return 301 https://$host:' . $WEBHTTPSPort . '$request_uri;' . PHP_EOL |
||
| 88 | . ' }' . PHP_EOL; |
||
| 89 | $config = str_replace('include mikopbx/locations/*.conf;', $conf_data, $config); |
||
| 90 | } |
||
| 91 | file_put_contents($httpConfigFile, $config); |
||
| 92 | |||
| 93 | // SSL |
||
| 94 | $WEBHTTPSPublicKey = $this->mikoPBXConfig->getGeneralSettings('WEBHTTPSPublicKey'); |
||
| 95 | $WEBHTTPSPrivateKey = $this->mikoPBXConfig->getGeneralSettings('WEBHTTPSPrivateKey'); |
||
| 96 | if ( |
||
| 97 | $not_ssl === false |
||
| 98 | && ! empty($WEBHTTPSPublicKey) |
||
| 99 | && ! empty($WEBHTTPSPrivateKey) |
||
| 100 | ) { |
||
| 101 | $public_filename = '/etc/ssl/certs/nginx.crt'; |
||
| 102 | $private_filename = '/etc/ssl/private/nginx.key'; |
||
| 103 | file_put_contents($public_filename, $WEBHTTPSPublicKey); |
||
| 104 | file_put_contents($private_filename, $WEBHTTPSPrivateKey); |
||
| 105 | $config = file_get_contents("{$httpsConfigFile}.original"); |
||
| 106 | $config = str_replace(['<DNS>', '<WEBHTTPSPort>'], [$dns_server, $WEBHTTPSPort], $config); |
||
| 107 | file_put_contents($httpsConfigFile, $config); |
||
| 108 | } elseif (file_exists($httpsConfigFile)) { |
||
| 109 | unlink($httpsConfigFile); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Test work |
||
| 113 | $currentConfigIsGood = $this->testCurrentNginxConfig(); |
||
| 114 | if ($level < 1 && ! $currentConfigIsGood) { |
||
| 115 | ++$level; |
||
| 116 | Util::sysLogMsg('nginx', 'Failed test config file. SSL will be disable...'); |
||
| 117 | $this->generateConf(true, $level); |
||
| 118 | } |
||
| 119 | // Add additional rules from modules |
||
| 120 | $this->generateModulesConf(); |
||
| 121 | } |
||
| 164 | } |