Completed
Push — master ( f85101...36fe64 )
by Pascal
10:14
created

ServerStatus::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 7
cts 7
cp 1
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php namespace MtGTutor\Console\Commands;
2
3
use Symfony\Component\Console\Command\Command;
4
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * ServerStatus Command
11
 * @package MtGTutor\Console\Commands
12
 */
13
class ServerStatus extends Command
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $host = 'www.mtg-tutor.de';
19
20
    /**
21
     * Configure command
22
     */
23 9
    protected function configure()
24
    {
25 9
        $this->setName('server:status')
26 9
            ->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port of the server', 80)
27 9
            ->setDescription("Check if the server of {$this->host} is online")
28 9
            ->setHelp(
29
                <<<EOT
30
Checks if the server is online
31
32
Usage:
33
34
You can specify a specific port with --port or -p.
35
<info>mtgtutor-console server:status --port=443</info>
36
<info>mtgtutor-console server:status -p 443</info>
37
38
If you don't specify a port number it will set by default [80]
39
<info>mtgtutor-console server:status</info>
40
EOT
41 9
            );
42 9
    }
43
44
    /**
45
     * Checks if server is online or not
46
     * @param \Symfony\Component\Console\Input\InputInterface   $input
47
     * @param \Symfony\Component\Console\Output\OutputInterface $output
48
     * @return void
49
     */
50 9
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52 9
        $port = $input->getOption('port');
53 9
        $message = "Host {$this->host} at Port {$port} is";
54
55
        // ping server
56 9
        $fsock = @fsockopen($this->host, $port, $errno, $errstr, 6);
57
58
        // Online
59 9
        if ($fsock) {
60 6
            $style = new OutputFormatterStyle('white', 'green', ['bold']);
61 6
            $output->getFormatter()->setStyle('okay', $style);
62 6
            $output->writeln("<okay>$message online</okay>");
63 6
        }
64
65
        // offline
66 9
        if (!$fsock) {
67 3
            $output->writeln("<error>$message offline</error>");
68
69
            // print fsockopen error
70 3
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
71 3
                $output->writeln("<info>$errstr ($errno)</info>");
72 3
            }
73 3
        }
74 9
    }
75
}
76