Completed
Push — master ( 8da12a...178c32 )
by Sébastien
14:05 queued 11:13
created

PjbServerStartCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

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