Completed
Pull Request — master (#108)
by Marek
02:47
created

Docker::getDockerIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Dock\Doctor;
4
5
use Dock\Doctor\Action\StartMachineOrInstall;
6
use Dock\IO\ProcessRunner;
7
use Dock\Installer\DockerInstaller;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class Docker extends Task
11
{
12
    /**
13
     * @var DockerInstaller
14
     */
15
    private $dockerInstaller;
16
17
    /**
18
     * @var StartMachineOrInstall
19
     */
20
    private $startMachineOrInstall;
21
22
    /**
23
     * @param ProcessRunner         $processRunner
24
     * @param StartMachineOrInstall $startMachineOrInstall
25
     * @param DockerInstaller       $dockerInstaller
26
     */
27
    public function __construct(ProcessRunner $processRunner, StartMachineOrInstall $startMachineOrInstall, DockerInstaller $dockerInstaller)
28
    {
29
        $this->processRunner = $processRunner;
30
        $this->dockerInstaller = $dockerInstaller;
31
        $this->startMachineOrInstall = $startMachineOrInstall;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function run(OutputInterface $output, $dryRun)
38
    {
39
        $this->handle(
40
            $output,
41
            'docker -v',
42
            'Docker is installed',
43
            'It seems docker is not installed.',
44
            'Install docker with `dock-cli docker:install`',
45
            $this->dockerInstaller,
46
            $dryRun
47
        );
48
49
        $this->handle(
50
            $output,
51
            'docker info',
52
            'Docker daemon is running',
53
            'It seems docker daemon is not running.',
54
            'Start restarting the Docker service or the VM',
55
            $this->startMachineOrInstall,
56
            $dryRun
57
        );
58
59
        $this->handle(
60
            $output,
61
            'ping -c1 '.$this->getDockerIp(),
62
            'Can ping docker virtual interface',
63
            "Can't ping docker virtual interface.",
64
            'Install and start docker by running: `dock-cli docker:install`',
65
            $this->dockerInstaller,
66
            $dryRun
67
        );
68
    }
69
70 View Code Duplication
    private function getDockerIp()
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...
71
    {
72
        $process = $this->processRunner->run("ip addr show docker0 | grep 'inet ' | awk -F\\  '{print $2}' | awk '{print $1}'");
73
        $network = explode('/', trim($process->getOutput()));
74
75
        return $network[0];
76
    }
77
}
78