Completed
Push — master ( 4456fe...59e4d4 )
by Sébastien
03:55
created

PjbServerStartCommand::execute()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0213

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
ccs 13
cts 15
cp 0.8667
rs 8.9713
cc 3
eloc 15
nc 2
nop 2
crap 3.0213
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 6
    protected function configure()
27
    {
28 6
        $this->setName('pjbserver:start')
29 6
             ->setDescription(
30
                 'Start the standalone pjb server (java)'
31 6
               )
32 6
            ->addArgument(
33 6
                'config-file',
34 6
                InputArgument::REQUIRED,
35
                'Configuration file, see ./dist/pjbserver.config.php.dist'
36 6
            )
37
38 6
             ->setHelp(<<<EOT
39
Start the php java bridge server in the background.
40
EOT
41 6
        );
42 6
    }
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 1
        $this->server->start();
64
65 1
        $output->writeln("Server successfully started on port $port");
66 1
        return 0;
67
    }
68
}
69