Skip to content

Commit f2f394e

Browse files
authored
Merge pull request #4 from cp6/dev
Dev to master 2.1
2 parents 92a19d6 + 1988da5 commit f2f394e

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ echo $vultr->listServers();//Lists all server instances for account
2525

2626
### Current version
2727

28-
2.0 Initial release (API v2) 7 Nov 2020
28+
2.0 Initial release (Vultr API v2) 7 Nov 2020
2929

3030
### Requires
3131

3232
Vultr API key obtained form your Vultr account menu.
3333

34-
Add the Vultr API key line 7: ```src/class.php```
34+
Add your Vultr API key line 7: ```src/class.php```
3535

3636
### Examples
3737

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=7.4"
20+
"php": ">=7.4",
21+
"ext-curl": "*"
2122
},
2223
"autoload": {
2324
"psr-4": {

src/class.php

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ class VultrAPI
1010
protected $server_create_details = [];
1111
protected $call_data;
1212

13-
public function __construct(bool $json_header = false)
13+
protected function apiKeyHeader(): array
1414
{
15-
if ($json_header) {
16-
header('Content-Type: application/json');
17-
}
15+
return array("Authorization: Bearer " . self::API_KEY, "Content-Type: application/json");
1816
}
1917

20-
public function apiKeyHeader(): array
21-
{
22-
return array("Authorization: Bearer " . self::API_KEY . "", "Content-Type: application/json");
23-
}
24-
25-
public function doCurl(string $url, string $type = 'GET', bool $return_http_code = false, array $headers = [], array $post_fields = [])
18+
protected function doCurl(string $url, string $type = 'GET', bool $return_http_code = false, array $headers = [], array $post_fields = [])
2619
{
2720
$crl = curl_init(self::API_URL . $url);
2821
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, $type);
@@ -48,13 +41,11 @@ public function doCurl(string $url, string $type = 'GET', bool $return_http_code
4841
curl_close($crl);
4942
if ($return_http_code) {
5043
return $http_response_code;
51-
} else {
52-
if ($http_response_code == 200 || $http_response_code == 201 || $http_response_code == 202) {
53-
return $this->call_data = $call_response;//Return data
54-
} else {
55-
return $this->call_data = array('http_response_code' => $http_response_code);//Call failed
56-
}
5744
}
45+
if ($http_response_code == 200 || $http_response_code == 201 || $http_response_code == 202) {
46+
return $this->call_data = $call_response;//Return data
47+
}
48+
return $this->call_data = array('http_response_code' => $http_response_code);//Call failed
5849
}
5950

6051
public function setSubid(string $instance_id): void
@@ -64,7 +55,7 @@ public function setSubid(string $instance_id): void
6455

6556
public function checkSubidSet()
6657
{
67-
if (is_null($this->instance_id) || empty($this->instance_id)) {
58+
if (empty($this->instance_id)) {
6859
return array("No subid is set, it is needed to perform this action.");
6960
}
7061
}
@@ -149,11 +140,7 @@ public function instanceUpdate(array $values = [])
149140
public function instanceReinstall(string $hostname = '')
150141
{
151142
$this->checkSubidSet();
152-
if (!empty($hostname)) {
153-
$post = array("hostname" => $hostname);
154-
} else {
155-
$post = array();
156-
}
143+
(!empty($hostname)) ? $post = array("hostname" => $hostname) : $post = array();
157144
return $this->doCurl("v2/instances/$this->instance_id/reinstall", 'POST', true, $this->apiKeyHeader(), $post);
158145
}
159146

@@ -365,20 +352,20 @@ public function serverCreatePlan(string $plan_id)
365352

366353
public function serverCreateType(string $type = 'OS', string $type_id = '1')
367354
{
368-
if ($type == 'OS') {
355+
if ($type === 'OS') {
369356
$this->server_create_details = array_merge($this->server_create_details, array(
370357
"os_id" => $type_id
371358
));
372-
} elseif ($type == 'SNAPSHOT') {
359+
} elseif ($type === 'SNAPSHOT') {
373360
$this->server_create_details = array_merge($this->server_create_details, array(
374361
"snapshot_id" => $type_id
375362
));
376-
} elseif ($type == 'ISO') {
363+
} elseif ($type === 'ISO') {
377364
$this->server_create_details = array_merge($this->server_create_details, array(
378365
"os_id" => 159,
379366
"iso_id" => $type_id
380367
));
381-
} elseif ($type == 'APP') {
368+
} elseif ($type === 'APP') {
382369
$this->server_create_details = array_merge($this->server_create_details, array(
383370
"os_id" => 186,
384371
"app_id" => $type_id
@@ -913,26 +900,24 @@ public function s3keyRegenObjectStorage(string $obj_id)
913900
*/
914901
public function convertBytes(int $bytes, string $convert_to = 'GB', bool $format = true, int $decimals = 2)
915902
{
916-
if ($convert_to == 'GB') {
903+
if ($convert_to === 'GB') {
917904
$value = ($bytes / 1073741824);
918-
} elseif ($convert_to == 'MB') {
905+
} elseif ($convert_to === 'MB') {
919906
$value = ($bytes / 1048576);
920-
} elseif ($convert_to == 'KB') {
907+
} elseif ($convert_to === 'KB') {
921908
$value = ($bytes / 1024);
922909
} else {
923910
$value = $bytes;
924911
}
925912
if ($format) {
926913
return number_format($value, $decimals);
927-
} else {
928-
return $value;
929914
}
915+
return $value;
930916
}
931917

932918
public function boolToInt(bool $bool): int
933919
{
934-
($bool) ? $int = 1 : $int = 0;
935-
return $int;
920+
return ($bool) ? 1 : 0;
936921
}
937922

938923
public function saveOutput(string $save_as, $output)

0 commit comments

Comments
 (0)