Completed
Push — master ( 7125ed...5f06a5 )
by Tyler
03:51
created

VultrRebooter::reboot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 3
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
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 View Code Duplication
class VultrRebooter extends ApiRebooter {
0 ignored issues
show
Duplication introduced by
This class 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...
9
10
    protected $token;
11
    protected $server_id;
12
    protected $host;
13
    protected $client;
14
    
15
    /**
16
     * @param string $token     API Token from vultr.com
17
     * @param number $server_id The ID of the server you want to control (Vultr calls it the `SUBID`)
18
     * @param string $host      The api host
19
     * @param Client $client    The guzzle client to use
20
     */
21 12
    public function __construct($token, $server_id, $host = "api.vultr.com", Client $client = null){
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("start");
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("halt");
55
    }
56
57
    /**
58
     * Builds the request URL for the API call
59
     * @param  string $action The Vultr API action
60
     * @return string
61
     */
62 6
    protected function buildRequestUrl($action){
63 6
        return "https://{$this->host}/v1/server/{$action}";
64
    }
65
66
    /**
67
     * Builds the request data for the API call
68
     * @param  string $action The Vultr API action
69
     * @return array
70
     */
71 3
    protected function buildRequestData($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...
72 3
        return ["SUBID"=>$this->server_id];
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
84 3
        return $this->client->request('POST', $url, [
85
            'headers' => [
86 3
                'API-Key' => $this->token,
87 3
            ],
88 3
            'form_params' => $data,
89 3
        ]);
90
    }
91
}