Completed
Pull Request — master (#30)
by Anton
17:34
created

StopCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 19
ccs 0
cts 12
cp 0
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 11
nc 4
nop 2
crap 12
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
 * StopCommand
16
 *
17
 * @package  Bluzman\Command\Server
18
 *
19
 * @author   Pavel Machekhin
20
 * @created  2013-06-17 14:47
21
 */
22
class StopCommand extends AbstractServerCommand
23
{
24
    /**
25
     * Command configuration
26
     */
27 14 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $this
30
            // the name of the command (the part after "bin/bluzman")
31 14
            ->setName('server:stop')
32
            // the short description shown while running "php bin/bluzman list"
33 14
            ->setDescription('Stop a built-in PHP server')
34
            // the full command description shown when running the command with
35
            // the "--help" option
36 14
            ->setHelp('This command allows you to stop built-in PHP server')
37
        ;
38 14
        $this->addOption('host', null, InputOption::VALUE_OPTIONAL, 'IP address of the server', '0.0.0.0');
39 14
        $this->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port of the server', '8000');
40 14
    }
41
42
    /**
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return int|null|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->info('Running "server:stop" command');
50
51
        $host = $input->getOption('host');
52
        $port = $input->getOption('port');
53
54
        $pid = $this->getProcessId($host, $port) ?: false;
55
56
        if (empty($pid)) {
57
            $this->comment('Server is not running');
58
            return;
59
        }
60
61
        $process = new Process("kill -9 $pid");
62
        $process->run();
63
64
        $this->write("Server <info>$host:$port</info> stopped");
65
    }
66
}
67