Completed
Push — master ( ba4810...40c16c )
by Henry
01:15
created

GecharlResponse::getBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created By: Henry Ejemuta
4
 * PC: Enrico Systems
5
 * Project: laravel-gecharl
6
 * Company: Stimolive Technologies Limited
7
 * Class Name: GecharlResponse.php
8
 * Date Created: 9/27/20
9
 * Time Created: 6:00 PM
10
 */
11
12
namespace HenryEjemuta\LaravelGecharl\Classes;
13
14
15
use HenryEjemuta\LaravelGecharl\Exceptions\GecharlErrorException;
16
17
class GecharlResponse
18
{
19
    private $message;
20
21
    /**
22
     * @var bool
23
     */
24
    private $hasError;
25
26
    /**
27
     * @var int
28
     */
29
    private $code;
30
31
    /**
32
     * Response Body from
33
     * @var object|null $body
34
     */
35
    private $body;
36
37
    const STATUS_MESSAGE = [
38
        "200" => '200 OK',
39
        "201" => '201 Created',
40
        "203" => '203 Non-Authoritative Information',
41
        "204" => '204 No Content',
42
        "301" => '301 Moved Permanently',
43
        "307" => '307 Temporary Redirect',
44
        "308" => '308 Permanent Redirect',
45
        "400" => '400 Bad Request',
46
        "401" => '401 Unauthorized',
47
        "402" => '402 Payment Required',
48
        "403" => '403 Forbidden',
49
        "404" => '404 Not Found',
50
        "408" => '408 Request Timeout',
51
        "413" => '413 Payload Too Large',
52
        "414" => '414 URI Too Long',
53
        "422" => '422 Unprocessable Entity',
54
        "500" => '500 Internal Server Error',
55
        "502" => '502 Bad Gateway',
56
        "503" => '503 Service Unavailable',
57
        "504" => '504 Gateway Timeout',
58
        "505" => '505 HTTP Version Not Supported',
59
    ];
60
61
62
    /**
63
     * GecharlResponse constructor.
64
     * @param int $code
65
     * @param object|array|null $responseBody
66
     * @throws GecharlErrorException
67
     */
68
    public function __construct(int $code, $responseBody = null)
69
    {
70
        $this->body = $responseBody;
0 ignored issues
show
Documentation Bug introduced by
It seems like $responseBody can also be of type array. However, the property $body is declared as type object|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
71
        $this->hasError = ($code == 200);
72
        $this->code = $code;
73
        $this->message = isset(self::STATUS_MESSAGE["{$this->code}"]) ? isset(self::STATUS_MESSAGE["{$this->code}"]) : 'Unable to determine response status.';
74
75
        if ($this->hasError)
76
            throw new GecharlErrorException($this->message, "$code");
0 ignored issues
show
Bug introduced by
It seems like $this->message can also be of type boolean; however, HenryEjemuta\LaravelGech...xception::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77
78
    }
79
80
    /**
81
     * Determine if this ise a success response object
82
     * @return bool
83
     */
84
    public function successful(): bool
85
    {
86
        return !($this->hasError);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getMessage(): string
93
    {
94
        return $this->message;
95
    }
96
97
    /**
98
     * @return object|array|null
99
     */
100
    public function getBody()
101
    {
102
        return $this->body;
103
    }
104
105
    public function __toString()
106
    {
107
        return json_encode($this->body);
108
    }
109
110
}
111