MachineIp   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toIP() 0 3 1
A fromMachine() 0 12 1
A fromIP() 0 6 1
A toString() 0 3 1
A __toString() 0 3 1
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