Completed
Push — master ( f7aa2f...852d76 )
by Sébastien
04:02
created

PjbServerStartCommand::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.4882

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 11
cts 16
cp 0.6875
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 3
nop 2
crap 4.4882
1
<?php
2
3
namespace PjbServer\Tools\Console\Command;
4
5
use PjbServer\Tools\StandaloneServer;
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
11
12
13
class PjbServerStartCommand extends Command
14
{
15
    use LoggerTrait;
16
17
    /**
18
     * @var StandaloneServer
19
     */
20
    protected $server;
21
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 7
    protected function configure()
27
    {
28 7
        $this->setName('pjbserver:start')
29 7
             ->setDescription(
30
                 'Start the standalone pjb server (java)'
31 7
               )
32 7
            ->addArgument(
33 7
                'config-file',
34 7
                InputArgument::REQUIRED,
35
                'Configuration file, see ./dist/pjbserver.config.php.dist'
36 7
            )
37
38 7
             ->setHelp(<<<EOT
39
Start the php java bridge server in the background.
40
EOT
41 7
        );
42 7
    }
43
44 1
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46 1
        $logger = $this->getConsoleLogger($output);
47
48 1
        $file = $input->getArgument('config-file');
49
50
        // Test if config file exists
51 1
        if (!file_exists($file) || !is_readable($file)) {
52
            $msg = "Configuration file '$file' does not exists or is not readable'";
53
            throw new \InvalidArgumentException($msg);
54
        }
55
56 1
        $params = include($file);
57 1
        $port = $params['port'];
58
59 1
        $config = new StandaloneServer\Config($params);
60 1
        $logger->notice("Starting the server on port '$port' and config file '$file'");
61 1
        $this->logServerConfig($logger, $config);
62 1
        $this->server = new StandaloneServer($config, $logger);
63
64 1
        if ($this->server->isProcessRunning()) {
65
            $pid = $this->server->getPid();
66
            $output->writeln("PjbServer is already running on port ${port} with pid {$pid}, skipping start");
67
        } else {
68
            $this->server->start();
69
            $output->writeln("Server successfully started on port $port");
70
        }
71
72
        return 0;
73
    }
74
}
75