1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Linode\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
use Linode\Api\Exceptions\ServerWasNotFoundException; |
8
|
|
|
use Linode\Api\Servers\Server; |
9
|
|
|
use Zurbaev\ApiClient\Commands\ApiCommand; |
10
|
|
|
use Zurbaev\ApiClient\Contracts\ApiProviderInterface; |
11
|
|
|
use Zurbaev\ApiClient\Contracts\ApiResourceInterface; |
12
|
|
|
use Zurbaev\ApiClient\Traits\AbstractCollection; |
13
|
|
|
use Zurbaev\ApiClient\Traits\LazyArrayAccess; |
14
|
|
|
use Zurbaev\ApiClient\Traits\LazyIterator; |
15
|
|
|
|
16
|
|
|
class Linode implements \ArrayAccess, \Iterator, ApiResourceInterface |
17
|
|
|
{ |
18
|
|
|
use AbstractCollection, LazyIterator, LazyArrayAccess; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ApiProviderInterface |
22
|
|
|
*/ |
23
|
|
|
protected $api; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $serversCache = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Linode constructor. |
32
|
|
|
* |
33
|
|
|
* @param ApiProviderInterface $api |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ApiProviderInterface $api) |
36
|
|
|
{ |
37
|
|
|
$this->api = $api; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getApi(): ApiProviderInterface |
41
|
|
|
{ |
42
|
|
|
return $this->api; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getHttpClient(): ClientInterface |
46
|
|
|
{ |
47
|
|
|
return $this->api->getClient(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function apiUrl(string $path = '', bool $withPropagation = true): string |
51
|
|
|
{ |
52
|
|
|
return 'linode/'.ltrim($path, '/'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function lazyLoad() |
56
|
|
|
{ |
57
|
|
|
$response = $this->getHttpClient()->request('GET', 'linode/instances'); |
58
|
|
|
$data = json_decode((string) $response->getBody(), true); |
59
|
|
|
|
60
|
|
|
$this->items = []; |
61
|
|
|
|
62
|
|
|
if (empty($data['linodes'])) { |
63
|
|
|
return $this->items; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($data['linodes'] as $server) { |
67
|
|
|
$this->items[$server['id']] = new Server($this->api, $server); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->items; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Generate items keys. |
75
|
|
|
*/ |
76
|
|
|
public function generateKeys() |
77
|
|
|
{ |
78
|
|
|
$this->keys = array_keys($this->items); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $serverId |
83
|
|
|
* @param bool $reload |
84
|
|
|
* |
85
|
|
|
* @return Server |
86
|
|
|
*/ |
87
|
|
|
public function get(int $serverId, bool $reload = false) |
88
|
|
|
{ |
89
|
|
|
if ($reload === true) { |
90
|
|
|
return $this->loadSingleServer($serverId); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($this->lazyLoadInitiated() && isset($this->items[$serverId])) { |
94
|
|
|
return $this->items[$serverId]; |
95
|
|
|
} elseif (isset($this->serversCache[$serverId])) { |
96
|
|
|
return $this->serversCache[$serverId]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->loadSingleServer($serverId); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function loadSingleServer(int $serverId) |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$response = $this->getHttpClient()->request('GET', 'linode/instances/'.$serverId); |
106
|
|
|
} catch (RequestException $e) { |
107
|
|
|
$response = $e->getResponse(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($response->getStatusCode() === 404) { |
111
|
|
|
throw new ServerWasNotFoundException('Server #'.$serverId.' was not found.', 404); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->serversCache[$serverId] = Server::createFromResponse($response, $this->api); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ApiCommand $command |
119
|
|
|
* |
120
|
|
|
* @return ApiCommand |
121
|
|
|
*/ |
122
|
|
|
public function execute(ApiCommand $command) |
123
|
|
|
{ |
124
|
|
|
return $command; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|