|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Laravel\Forge\ServerResources; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use Laravel\Forge\Server; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Laravel\Forge\Traits\ArrayAccessTrait; |
|
11
|
|
|
|
|
12
|
|
|
abstract class ServerResource implements ArrayAccess |
|
13
|
|
|
{ |
|
14
|
|
|
use ArrayAccessTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Associated server. |
|
18
|
|
|
* |
|
19
|
|
|
* @var \Laravel\Forge\Server |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $server; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Resource data. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $data = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create new resource instance. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Laravel\Forge\Server $server |
|
34
|
|
|
* @param array $data = [] |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Server $server, array $data = []) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->server = $server; |
|
39
|
|
|
$this->data = $data; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Resource type. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract public static function resourceType(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Resource path (relative to Server URL). |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
abstract public function resourcePath(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create new resource instance from HTTP response. |
|
58
|
|
|
* |
|
59
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
60
|
|
|
* @param \Laravel\Forge\Server $server |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function createFromResponse(ResponseInterface $response, Server $server) |
|
63
|
|
|
{ |
|
64
|
|
|
$data = json_decode((string) $response->getBody(), true); |
|
65
|
|
|
$resource = static::resourceType(); |
|
66
|
|
|
|
|
67
|
|
|
if (empty($data[$resource])) { |
|
68
|
|
|
throw new InvalidArgumentException('Given response is not a '.$resource.' response.'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return new static($server, $data[$resource]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Associated server. |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Laravel\Forge\Server |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getServer() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->server; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* HTTP Client from API provider. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \GuzzleHttp\ClientInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getHttpClient(): ClientInterface |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getServer()->getApi()->getClient(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get resource data. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string|int $key |
|
98
|
|
|
* @param mixed $default = null |
|
99
|
|
|
* |
|
100
|
|
|
* @return mixed|null |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getData($key, $default = null) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->data[$key] ?? $default; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Resource API URL. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function apiUrl($path = '') |
|
113
|
|
|
{ |
|
114
|
|
|
$path = ($path ? '/'.ltrim($path, '/') : ''); |
|
115
|
|
|
|
|
116
|
|
|
return $this->server->apiUrl( |
|
117
|
|
|
$this->resourcePath().'/'.$this->id().$path |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Resource ID. |
|
123
|
|
|
* |
|
124
|
|
|
* @return int |
|
125
|
|
|
*/ |
|
126
|
|
|
public function id(): int |
|
127
|
|
|
{ |
|
128
|
|
|
return intval($this->getData('id', 0)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Update resource data. |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function update(array $payload): bool |
|
137
|
|
|
{ |
|
138
|
|
|
$response = $this->getHttpClient()->request('PUT', $this->apiUrl(), [ |
|
139
|
|
|
'form_params' => $payload, |
|
140
|
|
|
]); |
|
141
|
|
|
|
|
142
|
|
|
$json = json_decode((string) $response->getBody(), true); |
|
143
|
|
|
$resource = static::resourceType(); |
|
144
|
|
|
|
|
145
|
|
|
if (empty($json[$resource])) { |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->data = $json[$resource]; |
|
150
|
|
|
|
|
151
|
|
|
return true; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Delete current resource. |
|
156
|
|
|
* |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function delete() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->getHttpClient()->request('DELETE', $this->apiUrl()); |
|
162
|
|
|
|
|
163
|
|
|
return true; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|