Completed
Push — master ( e03e34...021ac6 )
by Tyler
21:44 queued 19:31
created

DigitalOceanRebooter::reboot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tylercd100\Rebooter\Api;
4
5
use GuzzleHttp\Client;
6
use Tylercd100\Rebooter\ApiRebooter;
7
8
class DigitalOceanRebooter extends ApiRebooter {
9
10
    protected $token;
11
    protected $server_id;
12
    protected $host;
13
    protected $client;
14
    
15
    /**
16
     * @param string $token     API Token from DigitalOcean.com
17
     * @param number $server_id The ID of the droplet you want to control
18
     * @param string $host      The api host
19
     * @param Client $client    The guzzle client to use
20
     */
21 12 View Code Duplication
    public function __construct($token, $server_id, $host = "api.digitalocean.com", Client $client = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
22
23 12
        if(!$client instanceof Client){
24 9
            $client = new Client();
25 9
        }
26
27 12
        $this->client = $client;
28 12
        $this->token = $token;
29 12
        $this->server_id = $server_id;
30 12
        $this->host = $host;
31 12
    }
32
33
    /**
34
     * Executes a Boot command
35
     * @return \Psr\Http\Message\ResponseInterface
36
     */
37 3
    public function boot(){
38 3
        return $this->exec("power_on");
39
    }
40
41
    /**
42
     * Executes a Reboot command
43
     * @return \Psr\Http\Message\ResponseInterface
44
     */
45 6
    public function reboot(){
46 6
        return $this->exec("reboot");
47
    }
48
49
    /**
50
     * Executes a Shutdown command
51
     * @return \Psr\Http\Message\ResponseInterface
52
     */
53 3
    public function shutdown(){
54 3
        return $this->exec("power_off");
55
    }
56
57
    /**
58
     * Builds the request URL for the API call
59
     * @param  string $action The DigitalOcean API action
60
     * @return string
61
     */
62 6
    protected function buildRequestUrl($action){
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63 6
        return "https://{$this->host}/v2/droplets/{$this->server_id}/actions";
64
    }
65
66
    /**
67
     * Builds the request data for the API call
68
     * @param  string $action The DigitalOcean API action
69
     * @return array
70
     */
71 3
    protected function buildRequestData($action){
72 3
        return ["type"=>$action];
73
    }
74
75
    /**
76
     * Executes a command on the API
77
     * @param string $action
78
     * @return \Psr\Http\Message\ResponseInterface
79
     */
80 3
    protected function exec($action){
81 3
        $url  = $this->buildRequestUrl($action);
82 3
        $data = $this->buildRequestData($action);
83 3
        return $this->client->request('POST', $url, [
84 3
            'auth' => [$this->token,""],
85 3
            'form_params' => $data,
86 3
        ]);
87
    }
88
}