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

PjbServerStartCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 61
ccs 23
cts 28
cp 0.8214
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
B execute() 0 30 4
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