Failed Conditions
Pull Request — master (#68)
by Keoghan
04:22
created

MacOs   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 11
eloc 23
dl 0
loc 135
ccs 18
cts 36
cp 0.5
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isUsingDefaultHostAddress() 0 3 1
A trustCA() 0 7 1
A trustCertificate() 0 7 1
A getPorterDomainIp() 0 7 2
A setupNetworking() 0 7 1
A getUserHomePath() 0 3 1
A getHostAddress() 0 3 1
A flushDns() 0 4 1
A restoreNetworking() 0 7 1
A isUsingHostAddress() 0 3 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 $hostAddress Address for Host */
10
    protected $hostAddress = '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
     * Set up networking for Mac.
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 setupNetworking()
75
    {
76 1
        $this->consoleWriter->info("Adding loopback alias to {$this->hostAddress}/24. Please provide your sudo password.");
77
78 1
        $command = "sudo ifconfig lo0 alias {$this->hostAddress}/24";
79
80 1
        $this->cli->passthru($command);
81 1
    }
82
83
    /**
84
     * Restore networking on Mac.
85
     *
86
     * @return void
87
     */
88 1
    public function restoreNetworking()
89
    {
90 1
        $this->consoleWriter->info("Removing loopback alias to {$this->hostAddress}. Please provide your sudo password.");
91
92 1
        $command = "sudo ifconfig lo0 -alias {$this->hostAddress}";
93
94 1
        $this->cli->passthru($command);
95 1
    }
96
97
    /**
98
     * Return the host IP address in use.
99
     *
100
     * @return string
101
     */
102 1
    public function getHostAddress()
103
    {
104 1
        return $this->hostAddress;
105
    }
106
107
    /**
108
     * Does a Porter domain resolve to the Host Address
109
     *
110
     * @return bool
111
     * @throws UnableToRetrieveIP
112
     */
113
    public function isUsingHostAddress()
114
    {
115
        return $this->getPorterDomainIp() === $this->getHostAddress();
116
    }
117
118
    /**
119
     * Does a Porter domain resolve to 127.0.0.1
120
     *
121
     * @return bool
122
     * @throws UnableToRetrieveIP
123
     */
124
    public function isUsingDefaultHostAddress()
125
    {
126
        return $this->getPorterDomainIp() === '127.0.0.1';
127
    }
128
129
    /**
130
     * Determine the working IP for Porter
131
     *
132
     * @return string
133
     * @throws UnableToRetrieveIP
134
     */
135
    public function getPorterDomainIp()
136
    {
137
        if (($records = dns_get_record('www.unlikely-domain-name.'.setting('domain'))) === []) {
138
            throw new UnableToRetrieveIP;
139
        }
140
141
        return $records[0]['ip'];
142
    }
143
}
144