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

PjbServerStopCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 56
ccs 24
cts 26
cp 0.9231
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 25 3
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 6
    protected function configure()
27
    {
28 6
        $this->setName('pjbserver:stop')
29 6
             ->setDescription(
30
                 'Stop 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 6
             ->setHelp(<<<EOT
38
Stop the standalone php java bridge server (running in the background).
39
EOT
40 6
        );
41 6
    }
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);
62
63 2
        $this->server->stop();
64
65 2
        $output->writeln("Server running on port $port successfully stopped");
66 2
        return 0;
67
    }
68
}
69