Completed
Push — master ( 251c99...e0c0d7 )
by Timur
13s
created

Server::network()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Laravel\Forge;
4
5
use Laravel\Forge\Exceptions\Servers\PublicKeyWasNotFound;
6
use Laravel\Forge\Exceptions\Servers\ServerWasNotFoundException;
7
use Psr\Http\Message\ResponseInterface;
8
use Laravel\Forge\Contracts\ResourceContract;
9
10
class Server extends ApiResource
11
{
12
    /**
13
     * Resource type.
14
     *
15
     * @return string
16
     */
17
    public static function resourceType()
18
    {
19
        return 'server';
20
    }
21
22
    /**
23
     * Resource path (relative to owner or API root).
24
     *
25
     * @return string
26
     */
27
    public function resourcePath()
28
    {
29
        return 'servers';
30
    }
31
32
    /**
33
     * Throw HTTP Not Found exception.
34
     *
35
     * @throws \Exception
36
     */
37
    protected static function throwNotFoundException()
38
    {
39
        throw new ServerWasNotFoundException('Given response is not a server response.');
40
    }
41
42
    /**
43
     * Credential ID.
44
     *
45
     * @return int
46
     */
47
    public function credentialId(): int
48
    {
49
        return intval($this->getData('credential_id'));
50
    }
51
52
    /**
53
     * Human readable server size.
54
     *
55
     * @return string|null
56
     */
57
    public function size()
58
    {
59
        return $this->getData('size');
60
    }
61
62
    /**
63
     * Server region.
64
     *
65
     * @return string|null
66
     */
67
    public function region()
68
    {
69
        return $this->getData('region');
70
    }
71
72
    /**
73
     * Server's PHP version.
74
     *
75
     * @return string|null
76
     */
77
    public function phpVersion()
78
    {
79
        return $this->getData('php_version');
80
    }
81
82
    /**
83
     * Server public IP address.
84
     *
85
     * @return string|null
86
     */
87
    public function ip()
88
    {
89
        return $this->getData('ip_address');
90
    }
91
92
    /**
93
     * Server private IP address.
94
     */
95
    public function privateIp()
96
    {
97
        return $this->getData('private_ip_address');
98
    }
99
100
    /**
101
     * Server sudo password - only set on server save.
102
     */
103
    public function sudoPassword()
104
    {
105
        return $this->getData('sudo_password');
106
    }
107
108
    /**
109
     * Server sudo password - only set on server save.
110
     */
111
    public function databasePassword()
112
    {
113
        return $this->getData('database_password');
114
    }
115
116
    /**
117
     * Blackfire service status.
118
     *
119
     * @return string|null
120
     */
121
    public function blackfireStatus()
122
    {
123
        return $this->getData('blackfire_status');
124
    }
125
126
    /**
127
     * Papertrail service status.
128
     *
129
     * @return string
130
     */
131
    public function papertrailStatus()
132
    {
133
        return $this->getData('papertrail_status');
134
    }
135
136
    /**
137
     * Determines if server access was revoked from Forge.
138
     *
139
     * @return bool
140
     */
141
    public function isRevoked(): bool
142
    {
143
        return intval($this->getData('revoked')) === 1;
144
    }
145
146
    /**
147
     * Determines if server was provisioned and ready to use.
148
     *
149
     * @return bool
150
     */
151
    public function isReady(): bool
152
    {
153
        return intval($this->getData('is_ready')) === 1;
154
    }
155
156
    /**
157
     * Network status.
158
     *
159
     * @return array|null
160
     */
161
    public function network()
162
    {
163
        return $this->getData('network');
164
    }
165
166
    /**
167
     * Reboot the server.
168
     *
169
     * @return bool
170
     */
171
    public function reboot(): bool
172
    {
173
        $this->getHttpClient()->request('POST', $this->apiUrl('reboot'));
174
175
        return true;
176
    }
177
178
    /**
179
     * Revoke Forge access to server.
180
     *
181
     * @return bool
182
     **/
183
    public function revokeAccess(): bool
184
    {
185
        $this->getHttpClient()->request('POST', $this->apiUrl('/revoke'));
186
187
        return true;
188
    }
189
190
    /**
191
     * Reconnect revoked server.
192
     *
193
     * @return string Public SSH key.
194
     */
195
    public function reconnect(): string
196
    {
197
        $response = $this->getHttpClient()->request('POST', $this->apiUrl('/reconnect'));
198
        $json = json_decode((string) $response->getBody(), true);
199
200
        if (empty($json['public_key'])) {
201
            throw new PublicKeyWasNotFound(
202
                'Public key was not found after reconnecting revoked server (ID: '.$this->id().').',
203
                404
204
            );
205
        }
206
207
        return $json['public_key'];
208
    }
209
210
    /**
211
     * Reactivate revoked server. Make sure you've installed public SSH key
212
     * before calling this method.
213
     *
214
     * @return bool
215
     */
216
    public function reactivate(): bool
217
    {
218
        $this->getHttpClient()->request('POST', $this->apiUrl('/reactivate'));
219
220
        return true;
221
    }
222
223
    /**
224
     * Create new Resource instance from HTTP response.
225
     *
226
     * @param \Psr\Http\Message\ResponseInterface       $response
227
     * @param \Laravel\Forge\ApiProvider                $api
228
     * @param \Laravel\Forge\Contracts\ResourceContract $owner    = null
229
     */
230
    public static function createFromResponse(ResponseInterface $response, ApiProvider $api, ResourceContract $owner = null)
231
    {
232
        $json = json_decode((string) $response->getBody(), true);
233
234
        if (empty($json['server'])) {
235
            static::throwNotFoundException();
236
        } else {
237
            $result = $json['server'];
238
        }
239
240
        if (!empty($json['sudo_password'])) {
241
            $result['sudo_password'] = $json['sudo_password'];
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
242
        }
243
244
        if (!empty($json['database_password'])) {
245
            $result['database_password'] = $json['database_password'];
246
        }
247
248
        return new static($api, $result, $owner);
249
    }
250
}
251