MachineIp::toIP()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Machine;
4
5
use Cocotte\Environment\LazyEnvironmentValue;
6
use Cocotte\Shell\ProcessRunner;
7
use Darsyn\IP\IP;
8
use Symfony\Component\Process\Process;
9
10
class MachineIp implements LazyEnvironmentValue
11
{
12
    /**
13
     * @var IP
14
     */
15
    private $ip;
16
17
    public static function fromMachine(ProcessRunner $processRunner): self
18
    {
19
        $process = Process::fromShellCommandline(
20
            'docker-machine inspect '.
21
            '--format=\'{{.Driver.IPAddress}}\' "${MACHINE_NAME}"'
22
        );
23
24
        $processRunner->mustRun($process);
25
26
        $str = trim($process->getOutput());
27
28
        return self::fromIP(new IP($str));
29
    }
30
31
    public static function fromIP(IP $ip): self
32
    {
33
        $self = new self();
34
        $self->ip = $ip;
35
36
        return $self;
37
    }
38
39
    public function toIP(): IP
40
    {
41
        return $this->ip;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function toString(): string
48
    {
49
        return $this->ip->getShortAddress();
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function __toString(): string
56
    {
57
        return $this->toString();
58
    }
59
}
60