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

PjbServerStopCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
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 PjbServerStopCommand 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:stop')
26 7
             ->setDescription(
27
                 'Stop 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 7
             ->setHelp(<<<'EOT'
35
Stop the standalone php java bridge server (running in the background).
36
EOT
37 7
        );
38 7
    }
39
40 2
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42 2
        $logger = $this->getConsoleLogger($output);
43 2
        $file = $input->getArgument('config-file');
44
45
        // Test if config file exists
46 2
        if (!file_exists($file) || !is_readable($file)) {
47
            $msg = "Configuration file '$file' does not exists or is not readable'";
48
            throw new \InvalidArgumentException($msg);
49
        }
50 2
        $params = include $file;
51 2
        $port = $params['port'];
52
53 2
        $config = new StandaloneServer\Config($params);
54
55 2
        $logger->notice("Stopping the server on port '$port' and config file '$file'");
56 2
        $this->logServerConfig($logger, $config);
57
58 2
        $this->server = new StandaloneServer($config, $logger);
59
60 2
        $pid_file = $this->server->getConfig()->getPidFile();
61 2
        if (!file_exists($pid_file)) {
62 1
            $output->writeln("Server already stopped (pid_file '${pid_file}' not found).");
63 1
        } else {
64
            $this->server->stop();
65
            $output->writeln("Server running on port $port successfully stopped");
66
        }
67
68
        return 0;
69
    }
70
}
71