Completed
Pull Request — master (#3697)
by
unknown
65:32
created

PingCommand::ping()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 4
nop 2
1
<?php
2
3
namespace Doctrine\DBAL\Tools\Console\Command;
4
5
use Doctrine\DBAL\Connection;
6
use RuntimeException;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Throwable;
12
use function is_numeric;
13
use function sleep;
14
use function sprintf;
15
16
class PingCommand extends Command
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected static $defaultName = 'dbal:ping';
22
23
    protected function configure()
24
    {
25
        $this
26
            ->setDescription('Check db is available')
27
            ->addOption('limit', null, InputOption::VALUE_REQUIRED, 'Max number of pings to try', '1')
28
            ->addOption('sleep', null, InputOption::VALUE_REQUIRED, 'Length of time (seconds) to sleep between pings', '1');
29
    }
30
31
    protected function execute(InputInterface $input, OutputInterface $output) : int
32
    {
33
        $limit = $input->getOption('limit');
34
        if (! is_numeric($limit) || $limit < 0) {
35
            throw new RuntimeException('Option ""limit" must contain a positive integer value');
36
        }
37
        $sleep = $input->getOption('sleep');
38
        if (! is_numeric($sleep) || $sleep < 0) {
39
            throw new RuntimeException('Option "sleep" must contain a positive integer value');
40
        }
41
42
        return $this->waitForPing($this->getHelper('db')->getConnection(), (int) $limit, (int) $sleep, $output);
43
    }
44
45
    /**
46
     * @return int > 0 for error
47
     */
48
    private function waitForPing(Connection $conn, int $limit, int $sleep, OutputInterface $output) : int
49
    {
50
        while (true) {
51
            $last = $this->ping($conn, $output);
52
            if ($last === 0 || --$limit <= 0) {
53
                break;
54
            }
55
            sleep($sleep);
56
        }
57
58
        return $last;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $last does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
    }
60
61
    /**
62
     * @return int > 0 for error
63
     */
64
    private function ping(Connection $conn, OutputInterface $output) : int
65
    {
66
        try {
67
            if ($conn->ping()) {
68
                return 0;
69
            }
70
71
            $output->writeln('Ping failed');
72
73
            return 1;
74
        } catch (Throwable $e) {
75
            $output->writeln(sprintf('Ping failed: <error>%s</error>', $e->getMessage()));
76
77
            return 2;
78
        }
79
    }
80
}
81