| Total Complexity | 17 |
| Total Lines | 109 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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) |
||
| 100 | } |
||
| 101 | |||
| 102 | protected function loadSingleServer(int $serverId) |
||
| 103 | { |
||
| 104 | try { |
||
| 105 | $response = $this->getHttpClient()->request('GET', 'linode/instances/'.$serverId); |
||
| 106 | } catch (RequestException $e) { |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param ApiCommand $command |
||
| 119 | * |
||
| 120 | * @return ApiCommand |
||
| 121 | */ |
||
| 122 | public function execute(ApiCommand $command) |
||
| 127 |