Completed
Branch master (a8ecd1)
by Matt
08:35 queued 04:21
created

Action::getRequest()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 5
nop 2
dl 0
loc 21
ccs 0
cts 13
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
namespace Billow\Actions;
3
use GuzzleHttp\Psr7\Request;
4
use GuzzleHttp\Psr7\Stream;
5
use RuntimeException;
6
7
/**
8
 * @author Matt Frost<[email protected]>
9
 * @package Billow
10
 * @subpackage Actions
11
 * @license http://opensource.org/licenses/MIT MIT
12
 */
13
class Action implements ActionInterface
14
{
15
    /**
16
     * Endpoint for the action
17
     *
18
     * @const ENDPOINT
19
     */
20
    const ENDPOINT = 'https://api.digitalocean.com/v2/droplets/[:id:]/actions';
21
22
    /**
23
     * HTTP Method
24
     *
25
     * @const METHOD
26
     */
27
    const METHOD = 'POST';
28
29
    /**
30
     * ID of the droplet to perform the action on
31
     *
32
     * @var mixed $id
33
     */
34
    protected $id;
35
36
    /**
37
     * Set the droplet id
38
     *
39
     * @param mixed $id
40
     */
41
    public function setId($id)
42
    {
43
       $this->id = $id; 
44
    }
45
46
    /**
47
     * Method to return the action request object
48
     *
49
     * @param Array $headers
50
     * @param string $body
51
     * @return \GuzzleHttp\Message\Request
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Message\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
     */
53
    public function getRequest(Array $headers = [], $body = '')
54
    {
55
        if ($this->id === null) {
56
            throw new RuntimeException('You must provide the Droplet ID you want to perform an action on');
57
        }
58
59
        if (method_exists($this, 'getBody') && $body === '') {
60
            $body = $this->getBody();
61
        }
62
63
        if ($body === '' || json_decode($body, true) === []) {
64
            throw new RuntimeException('Body cannot be empty');
65
        }
66
67
        $endpoint = str_replace('[:id:]', $this->id, self::ENDPOINT);
68
69
        return new Request(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new GuzzleHttp\Ps...point, $headers, $body) returns the type GuzzleHttp\Psr7\Request which is incompatible with the documented return type GuzzleHttp\Message\Request.
Loading history...
70
            static::METHOD,
71
            $endpoint,
72
            $headers,
73
            $body
74
        );
75
    }
76
}
77