NodeJsServerCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SfCod\SocketIoBundle\Command;
4
5
use SfCod\SocketIoBundle\Service\Worker;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
12
/**
13
 * Class WorkSocketIoCommand
14
 * Socketio worker. Use pm2 (http://pm2.keymetrics.io/) for fork command.
15
 *
16
 * @package yiicod\socketio\commands
17
 */
18
class NodeJsServerCommand extends Command
19
{
20
    /**
21
     * @var Worker
22
     */
23
    protected $worker;
24
25
    /**
26
     * NodeJsServerCommand constructor.
27
     */
28
    public function __construct(Worker $worker)
29
    {
30
        $this->worker = $worker;
31
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Configure command.
37
     */
38
    protected function configure()
39
    {
40
        parent::configure();
41
42
        $this->setName('socket-io:node-js-server')
43
            ->setDescription('Work node-js server.')
44
            ->addOption('server', null, InputArgument::OPTIONAL, 'SocketIo server.', null)
45
            ->addOption('ssl', null, InputArgument::OPTIONAL, 'SSL certificate config.', null);
46
    }
47
48
    /**
49
     * Execute command.
50
     *
51
     * @return int|void|null
52
     *
53
     * @throws \Exception
54
     */
55
    public function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $io = new SymfonyStyle($input, $output);
58
59
        $io->success(sprintf('Worker daemon has been started.'));
60
61
        $process = $this->worker->nodeJs(
62
            $input->getOption('server') ?? getenv('SOCKET_IO_WS_SERVER'),
63
            $input->getOption('ssl') ?? getenv('SOCKET_IO_SSL') ? getenv('SOCKET_IO_SSL') : ''
64
        );
65
        $process->setIdleTimeout(false);
66
        $process->setTimeout(null);
67
        $process->run();
68
    }
69
}
70