Test Failed
Branch master (17185c)
by Keoghan
06:01 queued 02:00
created

MacOs::setupNetworking()   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
class MacOs extends Untrained
6
{
7
    /** @var string $hostAddress Address for Host */
8
    protected $hostAddress = '10.200.10.1';
9
10
    /**
11
     * Trust the given root certificate file in the Keychain.
12
     *
13
     * @param string $pem
14
     *
15
     * @return void
16
     */
17
    public function trustCA($pem)
18
    {
19
        $this->consoleWriter->info('Auto Trust CA Certificate, needs sudo privilege. Please provide your sudo password.');
20
21
        $command = "sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain {$pem}";
22
23
        $this->cli->passthru($command);
24
    }
25
26
    /**
27
     * Trust the given certificate file in the Mac Keychain.
28
     *
29
     * @param string $crt
30
     *
31
     * @return void
32
     */
33
    public function trustCertificate($crt)
34
    {
35
        $this->consoleWriter->info('Auto Trust Certificate, needs sudo privilege. Please provide your sudo password.');
36
37
        $command = "sudo security add-trusted-cert -d -r trustAsRoot -k /Library/Keychains/System.keychain {$crt}";
38
39
        $this->cli->passthru($command);
40
    }
41
42
    /**
43
     * Return the User's home directory path.
44
     *
45
     * @return string
46
     */
47 1
    public function getUserHomePath()
48
    {
49 1
        return $this->serverBag->get('HOME');
50
    }
51
52
    /**
53
     * Flush the host system DNS cache.
54
     *
55
     * @return void
56
     */
57 1
    public function flushDns()
58
    {
59 1
        $this->consoleWriter->info('Flushing DNS. Requires sudo permissions.');
60 1
        $this->cli->passthru('sudo killall -HUP mDNSResponder');
61 1
    }
62
63
    /**
64
     * Set up networking for Mac.
65
     *
66
     * Add a loopback alias to 10.200.10.1. This is then used as the IP for DNS resolution, otherwise
67
     * we get weird results when trying to access services hosted in docker (since they resolve
68
     * 127.0.0.1 to the requesting container).
69
     *
70
     * @return void
71
     */
72 1
    public function setupNetworking()
73
    {
74 1
        $this->consoleWriter->info("Adding loopback alias to {$this->hostAddress}/24. Please provide your sudo password.");
75
76 1
        $command = "sudo ifconfig lo0 alias {$this->hostAddress}/24";
77
78 1
        $this->cli->passthru($command);
79 1
    }
80
81
    /**
82
     * Restore networking on Mac.
83
     *
84
     * @return void
85
     */
86 1
    public function restoreNetworking()
87
    {
88 1
        $this->consoleWriter->info("Removing loopback alias to {$this->hostAddress}. Please provide your sudo password.");
89
90 1
        $command = "sudo ifconfig lo0 -alias {$this->hostAddress}";
91
92 1
        $this->cli->passthru($command);
93 1
    }
94
95
    /**
96
     * Return the host IP address in use.
97
     *
98
     * @return string
99
     */
100 1
    public function getHostAddress()
101
    {
102 1
        return $this->hostAddress;
103
    }
104
}
105