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

PjbServerStatusCommand::execute()   C

Complexity

Conditions 9
Paths 45

Size

Total Lines 58
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 9.9957

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 58
ccs 30
cts 39
cp 0.7692
rs 6.9928
cc 9
eloc 39
nc 45
nop 2
crap 9.9957

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PjbServer\Tools\Console\Command;
4
5
use PjbServer\Tools\Exception as PjbException;
6
use PjbServer\Tools\StandaloneServer;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
13
14
class PjbServerStatusCommand extends Command
15
{
16
    use LoggerTrait;
17
18
    /**
19
     * @var StandaloneServer
20
     */
21
    protected $server;
22
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 6
    protected function configure()
28
    {
29 6
        $this->setName('pjbserver:status')
30 6
             ->setDescription(
31
                 'Get the status of the standalone pjb server (java)'
32 6
               )
33 6
            ->addArgument(
34 6
                'config-file',
35 6
                InputArgument::REQUIRED,
36
                'Configuration file, see ./dist/pjbserver.config.php.dist'
37 6
            )
38
39 6
             ->setHelp(<<<EOT
40
Get the status of the php java bridge server in the background.
41
EOT
42 6
        );
43 6
    }
44
45 2
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47 2
        $logger = $this->getConsoleLogger($output);
48
49 2
        $file = $input->getArgument('config-file');
50
51
        // Test if config file exists
52 2
        if (!file_exists($file) || !is_readable($file)) {
53
            $msg = "Configuration file '$file' does not exists or is not readable'";
54
            throw new \InvalidArgumentException($msg);
55
        }
56
57 2
        $params = include($file);
58 2
        $port = $params['port'];
59
60 2
        $config = new StandaloneServer\Config($params);
61 2
        $this->logServerConfig($logger, $config);
62 2
        $this->server = new StandaloneServer($config, $logger);
63
64 2
        $pid_file = $config->getPidFile();
65 2
        $log_file = $config->getLogFile();
66
67 2
        if (file_exists($log_file)) {
68 1
            $output->writeln("---------------------------------------------------------");
69 1
            $output->writeln("Content of log file ($log_file)");
70 1
            $output->writeln("---------------------------------------------------------");
71 1
            $output->writeln($this->server->getOutput());
72 1
            $output->writeln("---------------------------------------------------------");
73 1
        }
74
75
76 2
        $isRunning = false;
77
        try {
78 2
            $pid = $this->server->getPid();
79
80 1
            if (!$this->server->isProcessRunning()) {
81
                $logger->error("Not running but pid file exists ($pid_file) and pid found ($pid)");
82
                $msg = "Server not running but pid exists (pid: $pid) in pid_file ($pid_file). Please restart.";
83
            } else {
84 1
                $isRunning = true;
85 1
                $msg = "Server is running on port '$port' (pid: $pid)";
86
            }
87 2
        } catch (PjbException\PidCorruptedException $e) {
88
            // Pid file corrupted
89
            $logger->critical("Cannot find server pid, your '$pid_file' is corrupted. Remove it.");
90
            $msg = "Server not running (Critical error: pid file corrupted)";
91 1
        } catch (PjbException\PidNotFoundException $e) {
92 1
            $logger->info("Pid file '$pid_file' not exists, assuming server is down.");
93 1
            $msg = "Server not running on port '$port' (no pid file found)";
94 1
        } catch (\Exception $e) {
95
            $logger->error('Unexpected exception when testing pid file.');
96
            $msg = "Cannot test server status";
97
        }
98
99 2
        $output->writeln($msg);
100
101 2
        return ($isRunning) ? 0 : 1;
102
    }
103
}
104