Passed
Push — master ( 3ee5ba...23bbf0 )
by Keoghan
04:26
created

MacOs::addAlternativeLoopbackAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App\Support\Mechanics;
4
5
use App\Support\Mechanics\Exceptions\UnableToRetrieveIP;
6
7
class MacOs extends Untrained
8
{
9
    /** @var string Alternative Loopback Address */
10
    protected $alternativeLoopback = '10.200.10.1';
11
12
    /**
13
     * Trust the given root certificate file in the Keychain.
14
     *
15
     * @param string $pem
16
     *
17
     * @return void
18
     */
19
    public function trustCA($pem)
20
    {
21
        $this->consoleWriter->info('Auto Trust CA Certificate, needs sudo privilege. Please provide your sudo password.');
22
23
        $command = "sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain {$pem}";
24
25
        $this->cli->passthru($command);
26
    }
27
28
    /**
29
     * Trust the given certificate file in the Mac Keychain.
30
     *
31
     * @param string $crt
32
     *
33
     * @return void
34
     */
35
    public function trustCertificate($crt)
36
    {
37
        $this->consoleWriter->info('Auto Trust Certificate, needs sudo privilege. Please provide your sudo password.');
38
39
        $command = "sudo security add-trusted-cert -d -r trustAsRoot -k /Library/Keychains/System.keychain {$crt}";
40
41
        $this->cli->passthru($command);
42
    }
43
44
    /**
45
     * Return the User's home directory path.
46
     *
47
     * @return string
48
     */
49 1
    public function getUserHomePath()
50
    {
51 1
        return $this->serverBag->get('HOME');
52
    }
53
54
    /**
55
     * Flush the host system DNS cache.
56
     *
57
     * @return void
58
     */
59 1
    public function flushDns()
60
    {
61 1
        $this->consoleWriter->info('Flushing DNS. Requires sudo permissions.');
62 1
        $this->cli->passthru('sudo killall -HUP mDNSResponder');
63 1
    }
64
65
    /**
66
     * Add the alternative loopback address to the system.
67
     *
68
     * Add a loopback alias to 10.200.10.1. This is then used as the IP for DNS resolution, otherwise
69
     * we get weird results when trying to access services hosted in docker (since they resolve
70
     * 127.0.0.1 to the requesting container).
71
     *
72
     * @return void
73
     */
74 1
    public function addAlternativeLoopbackAddress()
75
    {
76 1
        $this->consoleWriter->info("Adding loopback alias to {$this->alternativeLoopback}/24. Please provide your sudo password.");
77
78 1
        $command = "sudo ifconfig lo0 alias {$this->alternativeLoopback}/24";
79
80 1
        $this->cli->passthru($command);
81 1
    }
82
83
    /**
84
     * Remove the alternative loopback address from the system.
85
     *
86
     * @return void
87
     */
88 1
    public function removeAlternativeLoopbackAddress()
89
    {
90 1
        $this->consoleWriter->info("Removing loopback alias to {$this->alternativeLoopback}. Please provide your sudo password.");
91
92 1
        $command = "sudo ifconfig lo0 -alias {$this->alternativeLoopback}";
93
94 1
        $this->cli->passthru($command);
95 1
    }
96
97
    /**
98
     * Determine the working IP for Porter.
99
     *
100
     * @throws UnableToRetrieveIP
101
     *
102
     * @return string
103
     */
104
    public function getPorterDomainIp()
105
    {
106
        if (($records = dns_get_record('www.unlikely-domain-name.'.setting('domain'))) === []) {
107
            throw new UnableToRetrieveIP();
108
        }
109
110
        return $records[0]['ip'];
111
    }
112
}
113