ServerCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * This symfony command runs the websocket server for real-time notifications
4
 *
5
 * @package    BZiON
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
namespace BZIon\Command;
10
11
use BZIon\Session\DatabaseSessionHandler;
12
use BZIon\Socket\EventPusher;
13
use Ratchet\Server\IoServer;
14
use Ratchet\Session\SessionProvider;
15
use Ratchet\WebSocket\WsServer;
16
use React\EventLoop\Factory as EventLoopFactory;
17
use React\Socket\Server;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class ServerCommand extends Command
23
{
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('bzion:server')
28
            ->setDescription('Run notification server')
29
            ->addOption(
30
                'push',
31
                'p',
32
                InputOption::VALUE_OPTIONAL,
33
                'The push port'
34
            )
35
            ->addOption(
36
                'pull',
37
                'l',
38
                InputOption::VALUE_OPTIONAL,
39
                'The pull port'
40
            );
41
    }
42
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        // Make sure that websockets are enabled in the config so that the proper
46
        // session storage handler is used
47
        if (!$this->getContainer()->getParameter('bzion.features.websocket.enabled')) {
48
            $message = "You need to enable websockets in your config before using the push server";
49
            $output->writeln("<bg=red;options=bold>\n\n [ERROR] $message\n</>");
50
51
            return;
52
        }
53
54
        $loop = EventLoopFactory::create();
55
        $pusher = new EventPusher($loop, $output);
56
57
        $pushPort = ($input->getOption('push')) ?: $this->getContainer()
58
            ->getParameter('bzion.features.websocket.push_port');
59
60
        $pullPort = ($input->getOption('pull')) ?: $this->getContainer()
61
            ->getParameter('bzion.features.websocket.pull_port');
62
63
        $pullSocket = new Server($loop);
64
        $pullSocket->on('connection', function ($conn) use ($pusher) {
65
            $conn->on('data', function ($data) use ($pusher) {
66
                $pusher->onServerEvent(json_decode($data));
67
            });
68
        });
69
70
        // Bind to 127.0.0.1, so that only the server can send messages to the socket
71
        $pullSocket->listen($pullPort, '127.0.0.1');
72
        $output->writeln(" <fg=green>Running pull service on port $pullPort</>");
73
74
        $session = new SessionProvider(
75
                $pusher,
76
                new DatabaseSessionHandler()
77
            );
78
79
        $pushSocket = new Server($loop);
80
81
        $webServer = new IoServer(
0 ignored issues
show
Unused Code introduced by
$webServer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
            new WsServer(
83
                $session
84
            ),
85
            $pushSocket
86
        );
87
88
        // Binding to 0.0.0.0 means remotes can connect
89
        $pushSocket->listen($pushPort, '0.0.0.0');
90
        $output->writeln(" <fg=green>Running push service on port $pushPort</>");
91
92
        $output->writeln("\n <bg=green;options=bold>Welcome to the BZiON live notification server!</>");
93
        $loop->run();
94
    }
95
}
96