PjbServerRestartCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 26
ccs 12
cts 14
cp 0.8571
rs 9.7666
cc 4
nc 2
nop 2
crap 4.0466
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PjbServer\Tools\Console\Command;
6
7
use PjbServer\Tools\StandaloneServer;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class PjbServerRestartCommand extends Command
14
{
15
    use LoggerTrait;
16
17
    /**
18
     * @var StandaloneServer
19
     */
20
    protected $server;
21
22
    /**
23 7
     * {@inheritdoc}
24
     */
25 7
    protected function configure(): void
26 7
    {
27
        $this->setName('pjbserver:restart')
28 7
             ->setDescription(
29 7
                 'Restart the standalone pjb server (java)'
30 7
             )
31 7
             ->addArgument(
32
                 'config-file',
33 7
                 InputArgument::REQUIRED,
34 7
                 'Configuration file, see ./dist/pjbserver.config.php.dist'
35
             )
36
             ->setHelp(
37 7
                 <<<'EOT'
38 7
Start the php java bridge server in the background.
39
EOT
40 1
             );
41
    }
42 1
43
    protected function execute(InputInterface $input, OutputInterface $output)
44 1
    {
45
        $logger = $this->getConsoleLogger($output);
46
47 1
        $file = $input->getArgument('config-file');
48
49
        // Test if config file exists
50
        if (!is_string($file) || !file_exists($file) || !is_readable($file)) {
51 1
            $msg = sprintf("Configuration file '%s' does not exists or is not readable'", (string) json_encode($file));
52 1
            throw new \InvalidArgumentException($msg);
53 1
        }
54
        $params = include $file;
55 1
        $port = $params['port'];
56 1
        $config = new StandaloneServer\Config($params);
57
58 1
        $logger->notice("PJB server using port '$port' and config in '$file'");
59 1
        $this->logServerConfig($logger, $config);
60
61 1
        $this->server = new StandaloneServer($config, $logger);
62
        $this->server->restart();
63 1
64
        $logger->debug("Server output: \n" . $this->server->getOutput());
65 1
66
        $output->writeln("Server successfully restarted on port $port");
67
68
        return 0;
69
    }
70
}
71