| Conditions | 7 |
| Paths | 25 |
| Total Lines | 105 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 60 | public function respond(): ResponseInterface |
||
| 61 | { |
||
| 62 | $response = get_response(); |
||
| 63 | |||
| 64 | $httpStatusCodes = [ |
||
| 65 | kernel::RESPONSE_TEMPORARILY_DENY => [ |
||
| 66 | 'type' => 'captcha', |
||
| 67 | 'code' => kernel::HTTP_STATUS_FORBIDDEN, |
||
| 68 | ], |
||
| 69 | |||
| 70 | kernel::RESPONSE_LIMIT_SESSION => [ |
||
| 71 | 'type' => 'session_limitation', |
||
| 72 | 'code' => kernel::HTTP_STATUS_TOO_MANY_REQUESTS, |
||
| 73 | ], |
||
| 74 | |||
| 75 | kernel::RESPONSE_DENY => [ |
||
| 76 | 'type' => 'rejection', |
||
| 77 | 'code' => kernel::HTTP_STATUS_BAD_REQUEST, |
||
| 78 | ], |
||
| 79 | ]; |
||
| 80 | |||
| 81 | // Nothing happened. Return. |
||
| 82 | if (empty($httpStatusCodes[$this->result])) { |
||
| 83 | return $response; |
||
| 84 | } |
||
| 85 | |||
| 86 | $type = $httpStatusCodes[$this->result]['type']; |
||
| 87 | $statusCode = $httpStatusCodes[$this->result]['code']; |
||
| 88 | |||
| 89 | $viewPath = $this->getTemplate($type); |
||
|
|
|||
| 90 | |||
| 91 | // The language of output UI. It is used on views. |
||
| 92 | $langCode = get_session()->get('shieldon_ui_lang') ?? 'en'; |
||
| 93 | |||
| 94 | $showOnlineInformation = false; |
||
| 95 | $showUserInformation = false; |
||
| 96 | |||
| 97 | // Show online session count. It is used on views. |
||
| 98 | if (!empty($this->properties['display_online_info'])) { |
||
| 99 | $showOnlineInformation = true; |
||
| 100 | $onlineinfo = []; |
||
| 101 | |||
| 102 | $onlineinfo['queue'] = $this->sessionStatus['queue']; |
||
| 103 | $onlineinfo['count'] = $this->sessionStatus['count']; |
||
| 104 | $onlineinfo['period'] = $this->sessionLimit['period']; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Show user information such as IP, user-agent, device name. |
||
| 108 | if (!empty($this->properties['display_user_info'])) { |
||
| 109 | $showUserInformation = true; |
||
| 110 | $dialoguserinfo = []; |
||
| 111 | |||
| 112 | $dialoguserinfo['ip'] = $this->ip; |
||
| 113 | $dialoguserinfo['rdns'] = $this->rdns; |
||
| 114 | $dialoguserinfo['user_agent'] = get_request()->getHeaderLine('user-agent'); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Captcha form |
||
| 118 | $form = $this->getCurrentUrl(); |
||
| 119 | $captchas = $this->captcha; |
||
| 120 | |||
| 121 | $ui = [ |
||
| 122 | 'background_image' => '', |
||
| 123 | 'bg_color' => '#ffffff', |
||
| 124 | 'header_bg_color' => '#212531', |
||
| 125 | 'header_color' => '#ffffff', |
||
| 126 | 'shadow_opacity' => '0.2', |
||
| 127 | ]; |
||
| 128 | |||
| 129 | foreach (array_keys($ui) as $key) { |
||
| 130 | if (!empty($this->dialog[$key])) { |
||
| 131 | $ui[$key] = $this->dialog[$key]; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if (!defined('SHIELDON_VIEW')) { |
||
| 136 | define('SHIELDON_VIEW', true); |
||
| 137 | } |
||
| 138 | |||
| 139 | $css = include $this->getTemplate('css/default'); |
||
| 140 | |||
| 141 | ob_start(); |
||
| 142 | include $viewPath; |
||
| 143 | $output = ob_get_contents(); |
||
| 144 | ob_end_clean(); |
||
| 145 | |||
| 146 | // Remove unused variable notices generated from PHP intelephense. |
||
| 147 | unset( |
||
| 148 | $css, |
||
| 149 | $ui, |
||
| 150 | $form, |
||
| 151 | $captchas, |
||
| 152 | $langCode, |
||
| 153 | $showOnlineInformation, |
||
| 154 | $showUserInformation |
||
| 155 | ); |
||
| 156 | |||
| 157 | $stream = HttpFactory::createStream(); |
||
| 158 | $stream->write($output); |
||
| 159 | $stream->rewind(); |
||
| 160 | |||
| 161 | return $response |
||
| 162 | ->withHeader('X-Protected-By', 'shieldon.io') |
||
| 163 | ->withBody($stream) |
||
| 164 | ->withStatus($statusCode); |
||
| 165 | } |
||
| 214 |