DockerMachineCli::getIp()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
namespace Dock\Docker\Machine;
4
5
use Dock\IO\ProcessRunner;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
8
class DockerMachineCli implements Machine
9
{
10
    const DEFAULT_MACHINE_NAME = 'inviqa-dock-cli';
11
12
    /**
13
     * @var ProcessRunner
14
     */
15
    private $processRunner;
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @param ProcessRunner $processRunner
24
     * @param string        $name
25
     */
26
    public function __construct(ProcessRunner $processRunner, $name)
27
    {
28
        $this->processRunner = $processRunner;
29
        $this->name = $name;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 View Code Duplication
    public function isRunning()
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...
36
    {
37
        $process = $this->processRunner->run('docker-machine status '.$this->name, false);
38
        if (!$process->isSuccessful()) {
39
            return false;
40
        }
41
42
        return strpos($process->getOutput(), 'Running') === 0;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 View Code Duplication
    public function start()
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...
49
    {
50
        try {
51
            $this->processRunner->run('docker-machine start '.$this->name);
52
        } catch (ProcessFailedException $e) {
53
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 View Code Duplication
    public function stop()
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...
61
    {
62
        try {
63
            $this->processRunner->run('docker-machine stop '.$this->name);
64
        } catch (ProcessFailedException $e) {
65
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getIp()
73
    {
74
        try {
75
            $ip = $this->processRunner->run('docker-machine ip '.$this->name)->getOutput();
76
        } catch (ProcessFailedException $e) {
77
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
78
        }
79
80
        $ip = trim($ip);
81
        if (strpos($ip, 'not running') !== false) {
82
            throw new MachineException($ip);
83
        }
84
85
        return $ip;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 View Code Duplication
    public function isCreated()
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...
92
    {
93
        $process = $this->processRunner->run('docker-machine status '.$this->name, false);
94
        if(!$process->isSuccessful() || strpos($process->getOutput(), 'Error') === 0) {
95
            return false;
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 View Code Duplication
    public function create()
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...
105
    {
106
        try {
107
            $this->processRunner->run('docker-machine create --driver virtualbox '.$this->name);
108
        } catch (ProcessFailedException $e) {
109
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
110
        }
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getEnvironmentDeclaration()
117
    {
118
        return sprintf('eval "$(docker-machine env %s)"', $this->name).PHP_EOL;
119
    }
120
}
121