1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
4
|
|
|
// |
5
|
|
|
// Copyright (C) 2015 Artem Rodygin |
6
|
|
|
// |
7
|
|
|
// This file is part of Linode API Client Library for PHP. |
8
|
|
|
// |
9
|
|
|
// You should have received a copy of the MIT License along with |
10
|
|
|
// the library. If not, see <http://opensource.org/licenses/MIT>. |
11
|
|
|
// |
12
|
|
|
//---------------------------------------------------------------------- |
13
|
|
|
|
14
|
|
|
namespace Linode; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Basic class which can make calls to Linode API. |
18
|
|
|
*/ |
19
|
|
|
class BaseLinodeApi |
20
|
|
|
{ |
21
|
|
|
/** @var string API key */ |
22
|
|
|
protected $key; |
23
|
|
|
|
24
|
|
|
/** @var \Linode\Batch */ |
25
|
|
|
protected $batch; |
26
|
|
|
|
27
|
|
|
/** @var array Additional cURL options. */ |
28
|
|
|
protected $options; |
29
|
|
|
|
30
|
|
|
/** @var bool Whether the object is in debug mode */ |
31
|
|
|
protected $debug; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param string|Batch $key API key, or batch of requests |
37
|
|
|
* @param array $options Additional cURL options to be set. |
38
|
|
|
* @param bool $debug Whether the object should be in debug mode |
39
|
|
|
*/ |
40
|
72 |
|
public function __construct($key, array $options = [], $debug = false) |
41
|
|
|
{ |
42
|
72 |
|
if ($key instanceof Batch) { |
43
|
1 |
|
$this->batch = $key; |
44
|
1 |
|
} |
45
|
|
|
else { |
46
|
71 |
|
$this->key = $key; |
47
|
71 |
|
$this->batch = null; |
48
|
|
|
} |
49
|
|
|
|
50
|
72 |
|
$this->options = $options; |
51
|
72 |
|
$this->debug = $debug; |
52
|
72 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Performs specified call to Linode API. |
56
|
|
|
* |
57
|
|
|
* @param string $action |
58
|
|
|
* @param array $parameters |
59
|
|
|
* |
60
|
|
|
* @return array|bool |
61
|
|
|
* |
62
|
|
|
* @throws LinodeException |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
72 |
|
protected function call($action, array $parameters = []) |
66
|
|
|
{ |
67
|
72 |
|
if ($this->batch) { |
68
|
|
|
|
69
|
1 |
|
$query = ['api_action' => $action]; |
70
|
|
|
|
71
|
1 |
View Code Duplication |
foreach ($parameters as $key => $value) { |
|
|
|
|
72
|
1 |
|
if ($value !== null) { |
73
|
1 |
|
$query[urlencode($key)] = urlencode($value); |
74
|
1 |
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
$this->batch->addRequest($query); |
78
|
|
|
|
79
|
1 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
71 |
|
$curl = curl_init(); |
83
|
|
|
|
84
|
71 |
|
if (!$curl) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
71 |
|
$query = "api_key={$this->key}&api_action={$action}"; |
89
|
|
|
|
90
|
71 |
View Code Duplication |
foreach ($parameters as $key => $value) { |
|
|
|
|
91
|
66 |
|
if ($value !== null) { |
92
|
60 |
|
$query .= sprintf('&%s=%s', urlencode($key), urlencode($value)); |
93
|
60 |
|
} |
94
|
71 |
|
} |
95
|
|
|
|
96
|
71 |
|
if ($this->debug) { |
97
|
70 |
|
return $query; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$this->options[CURLOPT_URL] = 'https://api.linode.com/'; |
101
|
1 |
|
$this->options[CURLOPT_POST] = true; |
102
|
1 |
|
$this->options[CURLOPT_POSTFIELDS] = $query; |
103
|
1 |
|
$this->options[CURLOPT_RETURNTRANSFER] = true; |
104
|
|
|
|
105
|
1 |
|
curl_setopt_array($curl, $this->options); |
106
|
|
|
|
107
|
1 |
|
$result = curl_exec($curl); |
108
|
|
|
|
109
|
1 |
|
curl_close($curl); |
110
|
|
|
|
111
|
1 |
|
$json = json_decode($result, true); |
112
|
|
|
|
113
|
1 |
|
if (!$json) { |
114
|
|
|
throw new \RuntimeException('Empty response'); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$error = reset($json['ERRORARRAY']); |
118
|
|
|
|
119
|
1 |
|
if ($error) { |
120
|
|
|
throw new LinodeException($error['ERRORMESSAGE'], $error['ERRORCODE']); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return $json['DATA']; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.