Completed
Push — master ( dc78bb...d25275 )
by Timur
01:27
created

Server::disableOPCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
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 ServerWasNotFoundException
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
     * @return string|null
96
     */
97
    public function privateIp()
98
    {
99
        return $this->getData('private_ip_address');
100
    }
101
102
    /**
103
     * Server sudo password - only set on server save.
104
     *
105
     * @return string|null
106
     */
107
    public function sudoPassword()
108
    {
109
        return $this->getData('sudo_password');
110
    }
111
112
    /**
113
     * Server sudo password - only set on server save.
114
     *
115
     * @return string|null
116
     */
117
    public function databasePassword()
118
    {
119
        return $this->getData('database_password');
120
    }
121
122
    /**
123
     * Blackfire service status.
124
     *
125
     * @return string|null
126
     */
127
    public function blackfireStatus()
128
    {
129
        return $this->getData('blackfire_status');
130
    }
131
132
    /**
133
     * Papertrail service status.
134
     *
135
     * @return string
136
     */
137
    public function papertrailStatus()
138
    {
139
        return $this->getData('papertrail_status');
140
    }
141
142
    /**
143
     * Determines if server access was revoked from Forge.
144
     *
145
     * @return bool
146
     */
147
    public function isRevoked(): bool
148
    {
149
        return intval($this->getData('revoked')) === 1;
150
    }
151
152
    /**
153
     * Determines if server was provisioned and ready to use.
154
     *
155
     * @return bool
156
     */
157
    public function isReady(): bool
158
    {
159
        return intval($this->getData('is_ready')) === 1;
160
    }
161
162
    /**
163
     * Network status.
164
     *
165
     * @return array|null
166
     */
167
    public function network()
168
    {
169
        return $this->getData('network');
170
    }
171
172
    /**
173
     * Reboot the server.
174
     *
175
     * @return bool
176
     */
177
    public function reboot(): bool
178
    {
179
        $this->getHttpClient()->request('POST', $this->apiUrl('reboot'));
180
181
        return true;
182
    }
183
184
    /**
185
     * Enable PHP OPCache on the server.
186
     *
187
     * @return bool
188
     */
189
    public function enableOPCache(): bool
190
    {
191
        $this->getHttpClient()->request('POST', $this->apiUrl('/php/opcache'));
192
193
        return true;
194
    }
195
196
    /**
197
     * Disable PHP OPCache on the server.
198
     *
199
     * @return bool
200
     */
201
    public function disableOPCache(): bool
202
    {
203
        $this->getHttpClient()->request('DELETE', $this->apiUrl('/php/opcache'));
204
205
        return true;
206
    }
207
208
    /**
209
     * Revoke Forge access to server.
210
     *
211
     * @return bool
212
     **/
213
    public function revokeAccess(): bool
214
    {
215
        $this->getHttpClient()->request('POST', $this->apiUrl('/revoke'));
216
217
        return true;
218
    }
219
220
    /**
221
     * Reconnect revoked server.
222
     *
223
     * @return string Public SSH key.
224
     */
225
    public function reconnect(): string
226
    {
227
        $response = $this->getHttpClient()->request('POST', $this->apiUrl('/reconnect'));
228
        $json = json_decode((string) $response->getBody(), true);
229
230
        if (empty($json['public_key'])) {
231
            throw new PublicKeyWasNotFound(
232
                'Public key was not found after reconnecting revoked server (ID: '.$this->id().').',
233
                404
234
            );
235
        }
236
237
        return $json['public_key'];
238
    }
239
240
    /**
241
     * Reactivate revoked server. Make sure you've installed public SSH key
242
     * before calling this method.
243
     *
244
     * @return bool
245
     */
246
    public function reactivate(): bool
247
    {
248
        $this->getHttpClient()->request('POST', $this->apiUrl('/reactivate'));
249
250
        return true;
251
    }
252
253
    /**
254
     * Create new Resource instance from HTTP response.
255
     *
256
     * @param \Psr\Http\Message\ResponseInterface       $response
257
     * @param \Laravel\Forge\ApiProvider                $api
258
     * @param \Laravel\Forge\Contracts\ResourceContract $owner    = null
259
     *
260
     * @return static
261
     */
262
    public static function createFromResponse(ResponseInterface $response, ApiProvider $api, ResourceContract $owner = null)
263
    {
264
        $json = json_decode((string) $response->getBody(), true);
265
        $result = $json['server'] ?? null;
266
267
        if (is_null($result)) {
268
            static::throwNotFoundException();
269
        }
270
271
        if (!empty($json['sudo_password'])) {
272
            $result['sudo_password'] = $json['sudo_password'];
273
        }
274
275
        if (!empty($json['database_password'])) {
276
            $result['database_password'] = $json['database_password'];
277
        }
278
279
        return new static($api, $result, $owner);
280
    }
281
}
282