Completed
Push — master ( 550846...8c8483 )
by Anton
09:53
created

StatusCommand::execute()   B

Complexity

Conditions 5
Paths 17

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 27
rs 8.439
c 1
b 0
f 1
cc 5
eloc 16
nc 17
nop 2
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Server;
8
9
use Bluzman\Input\InputOption;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Process\Process;
13
14
/**
15
 * StatusCommand
16
 *
17
 * @category Command
18
 * @package  Bluzman
19
 *
20
 * @author   Pavel Machekhin
21
 * @created  2013-06-17 14:52
22
 */
23
class StatusCommand extends AbstractServerCommand
24
{
25
    /**
26
     * Command configuration
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            // the name of the command (the part after "bin/bluzman")
32
            ->setName('server:status')
33
            // the short description shown while running "php bin/bluzman list"
34
            ->setDescription('Get the status of built-in PHP server')
35
            // the full command description shown when running the command with
36
            // the "--help" option
37
            ->setHelp('This command allows you to check built-in PHP server')
38
        ;
39
        $this->addOption('host', null, InputOption::VALUE_OPTIONAL, 'IP address of the server', '0.0.0.0');
40
        $this->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port of the server', '8000');
41
    }
42
43
    /**
44
     * @param InputInterface $input
45
     * @param OutputInterface $output
46
     * @return int|null|void
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $this->info('Running "server:status" command');
51
        try {
52
            $host = $input->getOption('host');
53
            $port = $input->getOption('port');
54
55
            $pid = $this->getProcessId($host, $port) ?: false;
56
57
            if (!$pid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pid of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
58
                throw new NotRunningException;
59
            }
60
61
            $process = new Process("ps -p $pid -o comm=");
62
            $process->run();
63
64
            $processOutput = $process->getOutput();
65
66
            if (empty($processOutput)) {
67
                throw new NotRunningException;
68
            }
69
70
            $this->write("Server <info>$host:$port</info> is running. PID is <info>$pid</info>");
71
        } catch (NotRunningException $e) {
72
            $this->comment('Server is not running');
73
        }
74
    }
75
}
76