1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChrisArmitage\ScalewayApi\WebService\Servers; |
4
|
|
|
|
5
|
|
|
use ChrisArmitage\ScalewayApi\Client; |
6
|
|
|
|
7
|
|
|
class WebServiceGateway |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Client |
11
|
|
|
*/ |
12
|
|
|
protected $client; |
13
|
|
|
|
14
|
12 |
|
public function __construct(Client $client) { |
15
|
12 |
|
$this->client = $client; |
16
|
12 |
|
} |
17
|
|
|
|
18
|
2 |
View Code Duplication |
public function getServers() { |
|
|
|
|
19
|
|
|
try { |
20
|
2 |
|
$this->client->setResource('servers') |
21
|
2 |
|
->setMethod('GET'); |
22
|
2 |
|
$response = $this->client->call(); |
23
|
2 |
|
} catch (\Exception $e) { |
24
|
1 |
|
throw new GeneralException('GetServers call failed', 0, $e); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
return json_decode($response); |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
View Code Duplication |
public function getServer($serverId) { |
|
|
|
|
31
|
|
|
try { |
32
|
2 |
|
$this->client->setResource("servers/{$serverId}") |
33
|
2 |
|
->setMethod('GET'); |
34
|
2 |
|
$response = $this->client->call(); |
35
|
2 |
|
} catch (\Exception $e) { |
36
|
1 |
|
throw new GeneralException('GetServer call failed', 0, $e); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
return json_decode($response); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function createServer($name, $organizationId, $imageId, $commercialType) { |
43
|
|
|
try { |
44
|
2 |
|
$this->client->setResource('servers') |
45
|
2 |
|
->setMethod('POST') |
46
|
2 |
|
->setParameters( |
47
|
|
|
[ |
48
|
2 |
|
'name' => $name, |
49
|
2 |
|
'organization' => $organizationId, |
50
|
2 |
|
'image' => $imageId, |
51
|
2 |
|
'commercial_type' => $commercialType, |
52
|
|
|
] |
53
|
2 |
|
); |
54
|
2 |
|
$response = $this->client->call(); |
55
|
2 |
|
} catch (\Exception $e) { |
56
|
1 |
|
throw new GeneralException('CreateServer call failed', 0, $e); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return json_decode($response); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
View Code Duplication |
public function deleteServer($serverId) { |
|
|
|
|
63
|
|
|
try { |
64
|
2 |
|
$this->client->setResource("servers/{$serverId}") |
65
|
2 |
|
->setMethod('DELETE'); |
66
|
2 |
|
$response = $this->client->call(); |
67
|
2 |
|
} catch (\Exception $e) { |
68
|
1 |
|
throw new GeneralException('DeleteServer call failed', 0, $e); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return json_decode($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
View Code Duplication |
public function setAction($serverId, $action) { |
|
|
|
|
75
|
|
|
try { |
76
|
2 |
|
$this->client->setResource("servers/{$serverId}/action") |
77
|
2 |
|
->setMethod('POST') |
78
|
2 |
|
->setParameters( |
79
|
|
|
[ |
80
|
|
|
'action' => $action |
81
|
2 |
|
] |
82
|
2 |
|
); |
83
|
2 |
|
$response = $this->client->call(); |
84
|
2 |
|
} catch (\Exception $e) { |
85
|
1 |
|
throw new GeneralException('SetAction call failed', 0, $e); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return json_decode($response); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
View Code Duplication |
public function getUserData($serverId, $key) { |
|
|
|
|
92
|
|
|
try { |
93
|
2 |
|
$this->client->setResource("servers/{$serverId}/user_data/{$key}") |
94
|
2 |
|
->setMethod('GET'); |
95
|
2 |
|
$response = $this->client->call(); |
96
|
2 |
|
} catch (\Exception $e) { |
97
|
1 |
|
throw new GeneralException('GetUserData call failed', 0, $e); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $response; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.