Passed
Push — master ( aa7dec...caeac5 )
by Luiz Kim
02:38
created

WebSocketServerCommand.php$0 ➔ onOpen()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
1
<?php
2
3
namespace ControleOnline\Command;
4
5
use Ratchet\MessageComponentInterface;
0 ignored issues
show
Bug introduced by
The type Ratchet\MessageComponentInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Ratchet\ConnectionInterface;
0 ignored issues
show
Bug introduced by
The type Ratchet\ConnectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Ratchet\Server\IoServer;
0 ignored issues
show
Bug introduced by
The type Ratchet\Server\IoServer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Ratchet\Http\HttpServer;
0 ignored issues
show
Bug introduced by
The type Ratchet\Http\HttpServer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Ratchet\WebSocket\WsServer;
0 ignored issues
show
Bug introduced by
The type Ratchet\WebSocket\WsServer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class WebSocketServerCommand extends Command
15
{
16
    protected static $defaultName = 'app:websocket-server';
17
18
    protected function configure()
19
    {
20
        $this
21
            ->setDescription('Starts the WebSocket server')
22
            ->setHelp('This command starts a WebSocket server on port 8080');
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output): int
26
    {
27
        $output->writeln('Starting WebSocket server on port 8080...');
28
29
        $server = IoServer::factory(
30
            new HttpServer(
31
                new WsServer(
32
                    new class implements MessageComponentInterface {
33
                        protected \SplObjectStorage $clients;
34
35
                        public function __construct()
36
                        {
37
                            $this->clients = new \SplObjectStorage();
38
                        }
39
40
                        public function onOpen(ConnectionInterface $conn)
41
                        {
42
                            $this->clients->attach($conn);
43
                            echo "New connection! ({$conn->resourceId})\n";
44
                        }
45
46
                        public function onMessage(ConnectionInterface $from, $msg)
47
                        {
48
                            foreach ($this->clients as $client) {
49
                                if ($from !== $client) {
50
                                    $client->send($msg);
51
                                }
52
                            }
53
                        }
54
55
                        public function onClose(ConnectionInterface $conn)
56
                        {
57
                            $this->clients->detach($conn);
58
                            echo "Connection closed! ({$conn->resourceId})\n";
59
                        }
60
61
                        public function onError(ConnectionInterface $conn, \Exception $e)
62
                        {
63
                            echo "An error occurred: {$e->getMessage()}\n";
64
                            $conn->close();
65
                        }
66
                    }
67
                )
68
            ),
69
            8080
70
        );
71
72
        $server->run();
73
74
        return Command::SUCCESS;
75
    }
76
}