| Total Complexity | 75 |
| Total Lines | 539 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Server 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 Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Server implements MessageComponentInterface { |
||
| 32 | use |
||
| 33 | Singleton; |
||
| 34 | /** |
||
| 35 | * Message will be delivered to everyone |
||
| 36 | */ |
||
| 37 | const SEND_TO_ALL = 1; |
||
| 38 | /** |
||
| 39 | * Message will be delivered to registered users only |
||
| 40 | */ |
||
| 41 | const SEND_TO_REGISTERED_USERS = 2; |
||
| 42 | /** |
||
| 43 | * Message will be delivered to users, specified in target (might be array of users) |
||
| 44 | */ |
||
| 45 | const SEND_TO_SPECIFIC_USERS = 3; |
||
| 46 | /** |
||
| 47 | * Message will be delivered to users from group, specified in target (might be array of groups) |
||
| 48 | */ |
||
| 49 | const SEND_TO_USERS_GROUP = 4; |
||
| 50 | /** |
||
| 51 | * Message will be delivered to users whose connection objects have property with certain value, target should be an array with format [property, value] |
||
| 52 | */ |
||
| 53 | const SEND_TO_FILTER = 5; |
||
| 54 | /** |
||
| 55 | * Each object additionally will have properties `user_id`, `session_id`, `session_expire` and `user_groups` with user id and ids of user groups |
||
| 56 | * correspondingly |
||
| 57 | * |
||
| 58 | * @var ConnectionInterface[]|SplObjectStorage |
||
| 59 | */ |
||
| 60 | protected $clients; |
||
| 61 | /** |
||
| 62 | * @var ConnectionInterface[]|SplObjectStorage |
||
| 63 | */ |
||
| 64 | protected $servers; |
||
| 65 | /** |
||
| 66 | * Connection to master server |
||
| 67 | * |
||
| 68 | * @var Client_websocket |
||
| 69 | */ |
||
| 70 | protected $connection_to_master; |
||
| 71 | /** |
||
| 72 | * @var Pool |
||
| 73 | */ |
||
| 74 | protected $pool; |
||
| 75 | /** |
||
| 76 | * Address to WebSockets server in format wss://server/WebSockets or ws://server/WebSockets, so that one WebSockets server can reach another (in case of |
||
| 77 | * several servers) |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $address; |
||
| 82 | /** |
||
| 83 | * @var IoServer |
||
| 84 | */ |
||
| 85 | protected $io_server; |
||
| 86 | /** |
||
| 87 | * @var \React\EventLoop\LoopInterface |
||
| 88 | */ |
||
| 89 | protected $loop; |
||
| 90 | /** |
||
| 91 | * @var Client_connector |
||
| 92 | */ |
||
| 93 | protected $client_connector; |
||
| 94 | /** |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | protected $listen_port; |
||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $listen_locally; |
||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $dns_server; |
||
| 106 | /** |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | protected $security_key; |
||
| 110 | /** |
||
| 111 | * @var bool |
||
| 112 | */ |
||
| 113 | protected $remember_session_ip; |
||
| 114 | protected function construct () { |
||
| 115 | $Config = Config::instance(); |
||
| 116 | $Request = Request::instance(); |
||
| 117 | $module_data = $Config->module('WebSockets'); |
||
| 118 | $this->listen_port = $module_data->listen_port; |
||
| 119 | $this->listen_locally = $module_data->listen_locally ? '127.0.0.1' : '0.0.0.0'; |
||
| 120 | $this->dns_server = $module_data->dns_server ?: '127.0.0.1'; |
||
| 121 | $this->security_key = $module_data->security_key; |
||
| 122 | $this->remember_session_ip = $Config->core['remember_user_ip']; |
||
| 123 | $this->pool = Pool::instance(); |
||
| 124 | $this->clients = new SplObjectStorage; |
||
| 125 | $this->servers = new SplObjectStorage; |
||
| 126 | $this->address = ($Request->secure ? 'wss' : 'ws')."://$Request->host/WebSockets"; |
||
| 127 | } |
||
| 128 | /** |
||
| 129 | * Run WebSockets server |
||
| 130 | * |
||
| 131 | * @param null|string $address |
||
| 132 | */ |
||
| 133 | public function run ($address = null) { |
||
| 134 | $this->address = $address ?: $this->address; |
||
| 135 | @ini_set('error_log', LOGS.'/WebSockets-server.log'); |
||
| 136 | $ws_server = new WsServer($this); |
||
| 137 | $this->io_server = IoServer::factory( |
||
| 138 | new HttpServer( |
||
| 139 | new Connection_properties_injector($ws_server) |
||
| 140 | ), |
||
| 141 | $this->listen_port, |
||
| 142 | $this->listen_locally |
||
| 143 | ); |
||
| 144 | $this->loop = $this->io_server->loop; |
||
| 145 | $this->client_connector = new Client_connector( |
||
| 146 | $this->loop, |
||
| 147 | (new Dns_factory)->create( |
||
| 148 | $this->dns_server, |
||
| 149 | $this->loop |
||
| 150 | ) |
||
| 151 | ); |
||
| 152 | $this->connect_to_master(); |
||
| 153 | // Since we may work with a lot of different users - disable this cache in order to not run out of memory |
||
| 154 | User::instance()->disable_memory_cache(); |
||
| 155 | $this->loop->run(); |
||
| 156 | } |
||
| 157 | /** |
||
| 158 | * @param ConnectionInterface $connection |
||
| 159 | */ |
||
| 160 | public function onOpen (ConnectionInterface $connection) { |
||
| 161 | $this->clients->attach($connection); |
||
| 162 | } |
||
| 163 | /** |
||
| 164 | * @param ConnectionInterface $connection |
||
| 165 | * @param string $message |
||
| 166 | */ |
||
| 167 | public function onMessage (ConnectionInterface $connection, $message) { |
||
| 168 | $this->on_message_internal($connection, $message); |
||
| 169 | } |
||
| 170 | /** |
||
| 171 | * TODO: Probably split this into 2 methods |
||
| 172 | * |
||
| 173 | * @param Client_websocket|ConnectionInterface $connection |
||
| 174 | * @param string $message |
||
| 175 | */ |
||
| 176 | protected function on_message_internal ($connection, $message) { |
||
| 177 | $from_master = $connection === $this->connection_to_master; |
||
| 178 | if (!$this->parse_message($message, $action, $details, $send_to, $target)) { |
||
| 179 | if (!$from_master) { |
||
| 180 | $connection->close(); |
||
| 181 | } |
||
| 182 | return; |
||
| 183 | } |
||
| 184 | switch ($action) { |
||
| 185 | /** |
||
| 186 | * Connection to master server as server (by default all connections considered as clients) |
||
| 187 | */ |
||
| 188 | case "Server/connect:$this->security_key": |
||
| 189 | /** |
||
| 190 | * Under certain circumstances it may happen so that one server become available through multiple addresses, |
||
| 191 | * in this case we need to remove one of them from list of pools |
||
| 192 | */ |
||
| 193 | if ($details['from_slave'] === $this->address) { |
||
| 194 | $this->pool->del($details['to_master']); |
||
| 195 | $connection->close(); |
||
| 196 | } else { |
||
| 197 | $this->clients->detach($connection); |
||
| 198 | $this->servers->attach($connection); |
||
| 199 | } |
||
| 200 | return; |
||
| 201 | /** |
||
| 202 | * Internal connection from application |
||
| 203 | */ |
||
| 204 | case "Application/Internal:$this->security_key": |
||
| 205 | if ($this->parse_message($details, $action_, $details_, $send_to_, $target_)) { |
||
| 206 | $connection->close(); |
||
| 207 | $this->send_to_clients($action_, $details_, $send_to_, $target_); |
||
| 208 | } |
||
| 209 | return; |
||
| 210 | case 'Client/authentication': |
||
| 211 | $Session = Session::instance(); |
||
| 212 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 213 | $session = $Session->get($connection->session_id); |
||
| 214 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 215 | if ( |
||
| 216 | !$session || |
||
| 217 | !$Session->is_session_owner($session['id'], $connection->user_agent, $connection->remote_addr, $connection->ip) |
||
| 218 | ) { |
||
| 219 | $connection->send( |
||
| 220 | _json_encode(['Client/authentication:error', $this->compose_error(403)]) |
||
| 221 | ); |
||
| 222 | $connection->close(); |
||
| 223 | return; |
||
| 224 | } |
||
| 225 | $connection->user_id = $session['user']; |
||
| 226 | $connection->session_id = $session['id']; |
||
| 227 | $connection->session_expire = $session['expire']; |
||
| 228 | $connection->groups = User::instance()->get_groups($session['user']); |
||
| 229 | $connection->send( |
||
| 230 | _json_encode(['Client/authentication', 'ok']) |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | if ($from_master) { |
||
| 234 | $this->send_to_clients_internal($action, $details, $send_to, $target); |
||
| 235 | } elseif ($this->servers->contains($connection)) { |
||
| 236 | $this->broadcast_message_to_servers($message, $connection); |
||
| 237 | if (!$send_to) { |
||
| 238 | return; |
||
| 239 | } |
||
| 240 | $this->send_to_clients_internal($action, $details, $send_to, $target); |
||
| 241 | } elseif (property_exists($connection, 'user_id')) { |
||
| 242 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 243 | Event::instance()->fire( |
||
| 244 | 'WebSockets/message', |
||
| 245 | [ |
||
| 246 | 'action' => $action, |
||
| 247 | 'details' => $details, |
||
| 248 | 'language' => $connection->language, |
||
| 249 | 'user' => $connection->user_id, |
||
| 250 | 'session' => $connection->session_id, |
||
| 251 | 'connection' => $connection |
||
| 252 | ] |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | /** |
||
| 257 | * @param string $message |
||
| 258 | * @param string $action |
||
| 259 | * @param mixed $details |
||
| 260 | * @param int|int[] $send_to |
||
| 261 | * @param int $target |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | protected function parse_message ($message, &$action, &$details, &$send_to, &$target) { |
||
| 278 | } |
||
| 279 | /** |
||
| 280 | * @param string $message |
||
| 281 | * @param ConnectionInterface|null $skip_server |
||
| 282 | */ |
||
| 283 | protected function broadcast_message_to_servers ($message, $skip_server = null) { |
||
| 284 | foreach ($this->servers as $server) { |
||
| 285 | if ($server === $skip_server) { |
||
| 286 | continue; |
||
| 287 | } |
||
| 288 | $server->send($message); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | /** |
||
| 292 | * Compose error |
||
| 293 | * |
||
| 294 | * @param int $error_code HTTP status code |
||
| 295 | * @param null|string $error_message String representation of status code |
||
| 296 | * |
||
| 297 | * @return array Array to be passed as details to `::send_to_clients()` |
||
| 298 | */ |
||
| 299 | public function compose_error ($error_code, $error_message = null) { |
||
| 300 | $error_message = $error_message ?: status_code_string($error_code); |
||
| 301 | return [$error_code, $error_message]; |
||
| 302 | } |
||
| 303 | /** |
||
| 304 | * Send request to client |
||
| 305 | * |
||
| 306 | * @param string $action |
||
| 307 | * @param mixed $details |
||
| 308 | * @param int $send_to Constants `self::SEND_TO*` should be used here |
||
| 309 | * @param false|int|int[] $target Id or array of ids in case of response to one or several users or groups |
||
| 310 | */ |
||
| 311 | public function send_to_clients ($action, $details, $send_to, $target = false) { |
||
| 312 | $message = _json_encode([$action, $details, $send_to, $target]); |
||
| 313 | /** |
||
| 314 | * If server running in current process |
||
| 315 | */ |
||
| 316 | if ($this->io_server) { |
||
| 317 | if ($this->connection_to_master) { |
||
| 318 | $this->connection_to_master->send($message); |
||
| 319 | } else { |
||
| 320 | $this->broadcast_message_to_servers($message); |
||
| 321 | } |
||
| 322 | $this->send_to_clients_internal($action, $details, $send_to, $target); |
||
| 323 | return; |
||
| 324 | } |
||
| 325 | $servers = $this->pool->get_all(); |
||
| 326 | if ($servers) { |
||
| 327 | shuffle($servers); |
||
| 328 | $loop = Loop_factory::create(); |
||
| 329 | $connector = new Client_connector($loop); |
||
| 330 | $connector($servers[0])->then( |
||
| 331 | function (Client_websocket $connection) use ($message) { |
||
| 332 | $connection->send( |
||
| 333 | _json_encode(["Application/Internal:$this->security_key", $message]) |
||
| 334 | ); |
||
| 335 | // Connection will be closed by server itself, no need to stop loop here |
||
| 336 | }, |
||
| 337 | function () use ($loop) { |
||
| 338 | $loop->stop(); |
||
| 339 | } |
||
| 340 | ); |
||
| 341 | $loop->run(); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | /** |
||
| 345 | * Send request to client |
||
| 346 | * |
||
| 347 | * @param string $action |
||
| 348 | * @param mixed $details |
||
| 349 | * @param int $send_to Constants `self::SEND_TO_*` should be used here |
||
| 350 | * @param false|int|int[]|mixed[] $target Id or array of ids in case of response to one or several users or groups, [property, value] for filter |
||
| 351 | */ |
||
| 352 | protected function send_to_clients_internal ($action, $details, $send_to, $target = false) { |
||
| 412 | } |
||
| 413 | } |
||
| 414 | /** |
||
| 415 | * If session not expire - will send message, otherwise will disconnect |
||
| 416 | * |
||
| 417 | * @param ConnectionInterface $client |
||
| 418 | * @param string $message |
||
| 419 | */ |
||
| 420 | protected function send_to_client_if_not_expire ($client, $message) { |
||
| 421 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 422 | if ($client->session_expire >= time()) { |
||
| 423 | $client->send($message); |
||
| 424 | } else { |
||
| 425 | $client->close(); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | /** |
||
| 429 | * Close all client connections by specified session id |
||
| 430 | * |
||
| 431 | * @param string $session_id |
||
| 432 | */ |
||
| 433 | public function close_by_session ($session_id) { |
||
| 434 | $this->send_to_clients('Server/close_by_session', $session_id, 0); |
||
| 435 | } |
||
| 436 | /** |
||
| 437 | * Close all client connections by specified user id |
||
| 438 | * |
||
| 439 | * @param string $user_id |
||
| 440 | */ |
||
| 441 | public function close_by_user ($user_id) { |
||
| 443 | } |
||
| 444 | /** |
||
| 445 | * Connect to master server |
||
| 446 | * |
||
| 447 | * Two trials, if server do not respond twice - it will be removed from servers pool, and next server will become master |
||
| 448 | */ |
||
| 449 | protected function connect_to_master () { |
||
| 524 | } |
||
| 525 | ); |
||
| 526 | } |
||
| 527 | } |
||
| 528 | /** |
||
| 529 | * Get event loop instance |
||
| 530 | * |
||
| 531 | * @return \React\EventLoop\LoopInterface |
||
| 532 | */ |
||
| 533 | public function get_loop () { |
||
| 534 | return $this->loop; |
||
| 535 | } |
||
| 536 | /** |
||
| 537 | * @param ConnectionInterface $connection |
||
| 538 | */ |
||
| 539 | public function onClose (ConnectionInterface $connection) { |
||
| 540 | /** |
||
| 541 | * Generate pseudo-event when client is disconnected |
||
| 542 | */ |
||
| 543 | if (isset($connection->user_id) && $this->clients->contains($connection)) { |
||
| 544 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 545 | Event::instance()->fire( |
||
| 546 | 'WebSockets/message', |
||
| 547 | [ |
||
| 548 | 'action' => 'Client/disconnection', |
||
| 549 | 'details' => null, |
||
| 550 | 'language' => $connection->language, |
||
| 551 | 'user' => $connection->user_id, |
||
| 552 | 'session' => $connection->session_id, |
||
| 553 | 'connection' => $connection |
||
| 554 | ] |
||
| 555 | ); |
||
| 556 | } |
||
| 557 | // The connection is closed, remove it, as we can no longer send it messages |
||
| 558 | $this->clients->detach($connection); |
||
| 559 | $this->servers->detach($connection); |
||
| 560 | } |
||
| 561 | /** |
||
| 562 | * @param ConnectionInterface $connection |
||
| 563 | * @param Exception $e |
||
| 564 | */ |
||
| 565 | public function onError (ConnectionInterface $connection, Exception $e) { |
||
| 567 | } |
||
| 568 | public function __destruct () { |
||
| 569 | $this->pool->del($this->address); |
||
| 570 | } |
||
| 571 | } |
||
| 572 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths