PjbServerStopCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 32
c 2
b 1
f 0
dl 0
loc 59
ccs 26
cts 28
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 29 4
A configure() 0 13 1
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 PjbServerStopCommand 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:stop')
28 7
             ->setDescription(
29 7
                 'Stop 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
Stop the standalone php java bridge server (running in the background).
39
EOT
40 2
             );
41
    }
42 2
43 2
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $logger = $this->getConsoleLogger($output);
46 2
        $file = $input->getArgument('config-file');
47
48
        // Test if config file exists
49
        if (!file_exists($file) || !is_readable($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type null and string[]; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        if (!file_exists(/** @scrutinizer ignore-type */ $file) || !is_readable($file)) {
Loading history...
Bug introduced by
It seems like $file can also be of type null and string[]; however, parameter $filename of is_readable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        if (!file_exists($file) || !is_readable(/** @scrutinizer ignore-type */ $file)) {
Loading history...
50 2
            $msg = "Configuration file '$file' does not exists or is not readable'";
51 2
            throw new \InvalidArgumentException($msg);
52
        }
53 2
        $params = include $file;
54
        $port = $params['port'];
55 2
56 2
        $config = new StandaloneServer\Config($params);
57
58 2
        $logger->notice("Stopping the server on port '$port' and config file '$file'");
59
        $this->logServerConfig($logger, $config);
60 2
61 2
        $this->server = new StandaloneServer($config, $logger);
62 1
63 1
        $pid_file = $this->server->getConfig()->getPidFile();
64
        if (!file_exists($pid_file)) {
65
            $output->writeln("Server already stopped (pid_file '${pid_file}' not found).");
66
        } else {
67
            $this->server->stop();
68
            $output->writeln("Server running on port $port successfully stopped");
69
        }
70 1
71
        return 0;
72
    }
73
}
74