Completed
Push — master ( f7aa2f...852d76 )
by Sébastien
04:02
created

PjbServerStopCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 60
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 29 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
12
13
class PjbServerStopCommand extends Command
14
{
15
    use LoggerTrait;
16
17
    /**
18
     * @var StandaloneServer
19
     */
20
    protected $server;
21
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 7
    protected function configure()
27
    {
28 7
        $this->setName('pjbserver:stop')
29 7
             ->setDescription(
30
                 'Stop the standalone pjb server (java)'
31 7
               )
32 7
            ->addArgument(
33 7
                'config-file',
34 7
                InputArgument::REQUIRED,
35
                'Configuration file, see ./dist/pjbserver.config.php.dist'
36 7
            )
37 7
             ->setHelp(<<<EOT
38
Stop the standalone php java bridge server (running in the background).
39
EOT
40 7
        );
41 7
    }
42
43 2
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45 2
        $logger = $this->getConsoleLogger($output);
46 2
        $file = $input->getArgument('config-file');
47
48
        // Test if config file exists
49 2
        if (!file_exists($file) || !is_readable($file)) {
50
            $msg = "Configuration file '$file' does not exists or is not readable'";
51
            throw new \InvalidArgumentException($msg);
52
        }
53 2
        $params = include($file);
54 2
        $port = $params['port'];
55
56 2
        $config = new StandaloneServer\Config($params);
57
58 2
        $logger->notice("Stopping the server on port '$port' and config file '$file'");
59 2
        $this->logServerConfig($logger, $config);
60
61 2
        $this->server = new StandaloneServer($config, $logger);
62
63 2
        $pid_file = $this->server->getConfig()->getPidFile();
64 2
        if (!file_exists($pid_file)) {
65 1
            $output->writeln("Server already stopped (pid_file '${pid_file}' not found).");
66 1
        } else {
67
            $this->server->stop();
68
            $output->writeln("Server running on port $port successfully stopped");
69
        }
70
        return 0;
71
    }
72
}
73