Completed
Push — master ( a2b906...d5b295 )
by Peter
03:27
created

DigitalOcean::paramsToString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace PeterColes\Cluster\Servers\Adapters;
4
5
use Exception;
6
use PeterColes\Cluster\Contracts\Adapter;
7
use PeterColes\Cluster\Contracts\ServerAdapterInterface;
8
9
class DigitalOcean extends Adapter implements ServerAdapterInterface
10
{
11
    /**
12
     * The endpoint for all API calls to Digital Ocean (currently at v2)
13
     */
14
    protected $apiEndpoint = 'https://api.digitalocean.com/v2';
15
16
    /**
17
     * A set of default params for creating Digital Ocean droplets
18
     *
19
     * @todo add methods to make this more dynamic and controllable by consumers
20
     */
21
    protected $defaults = [
22
        "name" => "example.com",
23
        "region" => "lon1",
24
        "size" => "512mb",
25
        "image" => "ubuntu-14-04-x64",
26
        "ssh_keys" => null,
27
        "backups" => false,
28
        "ipv6" => true,
29
        "user_data" => null,
30
        "private_networking" => null
31
    ];
32
33
    /**
34
     * List details for an indexed server, or all servers if id is null.
35
     *
36
     * @param integer $id
37
     * @return GuzzleResponse object | array of GuzzleResponse objects
38
     */
39
    public function read($id = null)
40
    {
41
        if ($id === null) {
42
            $response = $this->client->request->get($this->apiEndpoint."/droplets");
43
        } else {
44
            $response = $this->client->request->get($this->apiEndpoint."/droplets/$id");
45
        }
46
47
        return $this->client->getBody($response);
48
    }
49
50
    /**
51
     * Create a new server based on parameters received.
52
     *
53
     * @param array $params
54
     * @return array
55
     */
56
    public function create($params = array())
57
    {
58
        $serverConfig = array_merge($this->defaults, $params);
59
60
        try {
61
            $response = $this->client->request->post($this->apiEndpoint."/droplets", ['form_params' => $serverConfig]);
62
63
            if (202 != $this->client->getStatus($response)) {
64
                throw new Exception('Unable to create server.');
65
            }
66
        } catch (Exception $e) {
67
            echo 'Unable to create server because '.$e->getMessage();
68
        }
69
70
        return $this->client->getBody($response);
71
    }
72
73
    /**
74
     * Delete the server corresponding to the given id.
75
     *
76
     * @param integer $id
77
     * @return integer | null
78
     */
79
    public function delete($id)
80
    {
81
        try {
82
            $response = $this->client->request->delete($this->apiEndpoint."/droplets/$id");
83
84
            $status = $this->client->getStatus($response);
85
86
            if (204 != $status) {
87
                throw new Exception('Digital Ocean responded that it could not delete it.');
88
            }
89
90
            return $status;
91
92
        } catch (Exception $e) {
93
            echo 'Unable to delete server because '.$e->getMessage();
94
        }
95
    }
96
97
    /**
98
     * List of available snapshots.
99
     *
100
     * @param array $params
101
     * @return array
102
     */
103
    public function images($params)
104
    {
105
        try {
106
            $response = $this->client->request->get($this->apiEndpoint.'/images'.$this->paramsToString($params));
107
108
            $status = $this->client->getStatus($response);
109
110
            if (200 != $status) {
111
                throw new Exception('Digital Ocean was not able to successfully provide a list of snapshots.');
112
            }
113
114
            return $this->client->getBody($response);
115
116
        } catch (Exception $e) {
117
            echo 'Unable to list snapshots because '.$e->getMessage();
118
        }
119
    }
120
121
    /**
122
     * Construct http client request headers.
123
     *
124
     * @return void
125
     */
126
    protected function setHeaders()
127
    {
128
        parent::setHeaders();
129
130
        $this->headers['headers']['Authorization'] = 'Bearer '.$this->params['token'];
131
    }
132
133
    /**
134
     * Construct http client request headers.
135
     * @todo this isn't the best place for this method - see where it gets used next and find it a better home
136
     *
137
     * @param array | null  $in   array of parameters to be converted to string for URL
138
     * @return string
139
     */
140
    private function paramsToString($in)
141
    {
142
        $out = http_build_query((array) $in);
143
144
        return strlen($out) > 0 ? '?'.$out : '';
145
    }
146
}
147