Completed
Push — master ( 0ac51f...eeaba4 )
by Timur
02:26
created

Server::size()   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 ArrayAccess;
6
use Psr\Http\Message\ResponseInterface;
7
use Laravel\Forge\Traits\ArrayAccessTrait;
8
use Laravel\Forge\Exceptions\Servers\PublicKeyWasNotFound;
9
use Laravel\Forge\Exceptions\Servers\ServerWasNotFoundException;
10
11
class Server implements ArrayAccess
12
{
13
    use ArrayAccessTrait;
14
15
    /**
16
     * API provider.
17
     *
18
     * @var \Laravel\Forge\ApiProvider
19
     */
20
    protected $api;
21
22
    /**
23
     * Server data.
24
     *
25
     * @var array
26
     */
27
    protected $data;
28
29
    /**
30
     * Create new server instance.
31
     *
32
     * @param \Laravel\Forge\ApiProvider $api  = null
33
     * @param array                      $data = []
34
     */
35
    public function __construct(ApiProvider $api = null, array $data = [])
36
    {
37
        $this->api = $api;
38
        $this->data = $data;
39
    }
40
41
    /**
42
     * Create new server instance from HTTP response.
43
     *
44
     * @param \Psr\Http\Message\ResponseInterface $response
45
     * @param \Laravel\Forge\ApiProvider          $api      = null
46
     *
47
     * @throws \Laravel\Forge\Exceptions\Servers\ServerWasNotFoundException
48
     *
49
     * @return static
50
     */
51
    public static function createFromResponse(ResponseInterface $response, ApiProvider $api = null)
52
    {
53
        $json = json_decode((string) $response->getBody(), true);
54
55
        if (empty($json['server'])) {
56
            throw new ServerWasNotFoundException('Given response is not a server response.');
57
        }
58
59
        return new static($api, $json['server']);
60
    }
61
62
    /**
63
     * Server data.
64
     *
65
     * @param string $key     = null
66
     * @param mixed  $default = null
67
     *
68
     * @return mixed|array|null
69
     */
70
    protected function serverData(string $key = null, $default = null)
71
    {
72
        if (is_null($key)) {
73
            return $this->data;
74
        }
75
76
        return $this->data[$key] ?? $default;
77
    }
78
79
    /**
80
     * API provider.
81
     *
82
     * @return \Laravel\Forge\ApiProvider
83
     */
84
    public function getApi(): ApiProvider
85
    {
86
        return $this->api;
87
    }
88
89
    /**
90
     * Server ID.
91
     *
92
     * @return int
93
     */
94
    public function id(): int
95
    {
96
        return intval($this->serverData('id'));
97
    }
98
99
    /**
100
     * Credential ID.
101
     *
102
     * @return int
103
     */
104
    public function credentialId(): int
105
    {
106
        return intval($this->serverData('credential_id'));
107
    }
108
109
    /**
110
     * Server name.
111
     *
112
     * @return string|null
113
     */
114
    public function name()
115
    {
116
        return $this->serverData('name');
117
    }
118
119
    /**
120
     * Human readable server size.
121
     *
122
     * @return string|null
123
     */
124
    public function size()
125
    {
126
        return $this->serverData('size');
127
    }
128
129
    /**
130
     * Server region.
131
     *
132
     * @return string|null
133
     */
134
    public function region()
135
    {
136
        return $this->serverData('region');
137
    }
138
139
    /**
140
     * Server's PHP version.
141
     *
142
     * @return string|null
143
     */
144
    public function phpVersion()
145
    {
146
        return $this->serverData('php_version');
147
    }
148
149
    /**
150
     * Server public IP address.
151
     *
152
     * @return string|null
153
     */
154
    public function ip()
155
    {
156
        return $this->serverData('ip_address');
157
    }
158
159
    /**
160
     * Server private IP address.
161
     */
162
    public function privateIp()
163
    {
164
        return $this->serverData('private_ip_address');
165
    }
166
167
    /**
168
     * Blackfire service status.
169
     *
170
     * @return string|null
171
     */
172
    public function blackfireStatus()
173
    {
174
        return $this->serverData('blackfire_status');
175
    }
176
177
    /**
178
     * Papertrail service status.
179
     *
180
     * @return string
181
     */
182
    public function parerTrailStatus()
183
    {
184
        return $this->serverData('papertrail_status');
185
    }
186
187
    /**
188
     * Determines if server access was revoked from Forge.
189
     *
190
     * @return bool
191
     */
192
    public function isRevoked(): bool
193
    {
194
        return intval($this->serverData('revoked')) === 1;
195
    }
196
197
    /**
198
     * Determines if server was provisioned and ready to use.
199
     *
200
     * @return bool
201
     */
202
    public function isReady(): bool
203
    {
204
        return intval($this->serverData('is_ready')) === 1;
205
    }
206
207
    /**
208
     * Network status.
209
     *
210
     * @return array|null
211
     */
212
    public function network()
213
    {
214
        return $this->serverData('network');
215
    }
216
217
    /**
218
     * Server's API URL.
219
     *
220
     * @param string $path = ''
221
     *
222
     * @return string
223
     */
224
    public function apiUrl(string $path = ''): string
225
    {
226
        $path = ($path ? '/'.ltrim($path, '/') : '');
227
228
        return 'servers/'.$this->id().$path;
229
    }
230
231
    /**
232
     * Update server data.
233
     *
234
     * @param array $payload
235
     *
236
     * @return bool
237
     */
238
    public function update(array $payload): bool
239
    {
240
        ksort($payload);
241
242
        $response = $this->api->getClient()->request('PUT', $this->apiUrl(), [
243
            'form_params' => $payload,
244
        ]);
245
246
        $json = json_decode((string) $response->getBody(), true);
247
248
        if (empty($json['server'])) {
249
            return false;
250
        }
251
252
        $this->data = $json['server'];
253
254
        return true;
255
    }
256
257
    /**
258
     * Reboot the server.
259
     *
260
     * @return bool
261
     */
262
    public function reboot(): bool
263
    {
264
        $this->api->getClient()->request('POST', $this->apiUrl('reboot'));
265
266
        return true;
267
    }
268
269
    /**
270
     * Revoke Forge access to server.
271
     *
272
     * @return bool
273
     **/
274
    public function revokeAccess(): bool
275
    {
276
        $this->api->getClient()->request('POST', $this->apiUrl('/revoke'));
277
278
        return true;
279
    }
280
281
    /**
282
     * Reconnect revoked server.
283
     *
284
     * @return string Public SSH key.
285
     */
286
    public function reconnect(): string
287
    {
288
        $response = $this->api->getClient()->request('POST', $this->apiUrl('/reconnect'));
289
        $json = json_decode((string) $response->getBody(), true);
290
291
        if (empty($json['public_key'])) {
292
            throw new PublicKeyWasNotFound(
293
                'Public key was not found after reconnecting revoked server (ID: '.$this->id().').',
294
                404
295
            );
296
        }
297
298
        return $json['public_key'];
299
    }
300
301
    /**
302
     * Reactivate revoked server. Make sure you've installed public SSH key
303
     * before calling this method.
304
     *
305
     * @return bool
306
     */
307
    public function reactivate(): bool
308
    {
309
        $this->api->getClient()->request('POST', $this->apiUrl('/reactivate'));
310
311
        return true;
312
    }
313
314
    /**
315
     * Delete the server.
316
     *
317
     * @return bool
318
     */
319
    public function delete(): bool
320
    {
321
        $this->api->getClient()->request('DELETE', $this->apiUrl());
322
323
        return true;
324
    }
325
}
326