Completed
Push — master ( a8bf63...814c7f )
by Henry
10:23
created

GecharlResponse   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 100
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 6
A successful() 0 4 1
A getTitle() 0 4 1
A getMessage() 0 4 1
A getBody() 0 4 1
A __toString() 0 4 1
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
20
    /**
21
     * @var bool
22
     */
23
    private $hasError;
24
25
    /**
26
     * @var string $title
27
     */
28
    private $title;
29
30
    /**
31
     * Response Message as determined by status code
32
     * @var string $message
33
     */
34
    private $message;
35
36
    /**
37
     * Response Body from
38
     * @var object|null $body
39
     */
40
    private $body;
41
42
    /**
43
     * @var array $additionalStatusDetails
44
     */
45
    private $additionalStatus;
46
47
    /**
48
     * GecharlResponse constructor.
49
     * @param string $code
50
     * @param object|array|null $responseBody
51
     * @throws GecharlErrorException
52
     */
53
    public function __construct(string $code, $responseBody = null)
54
    {
55
        $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...
56
        $this->additionalStatus = [];
57
        $this->title = "Empty Response";
58
        $this->message = "Empty Response from VTPass server";
59
        $this->hasError = false;
60
61
        if (isset(GecharlResponse::RESPONSE["$code"])) {
62
            $msg = GecharlResponse::RESPONSE["$code"];
63
            $this->title = $msg['title'];
64
            $this->message = $msg['message'];
65
            $this->hasError = $msg['error'];
66
            if ("$code" === "000" && isset($responseBody->status)) {
67
                if (isset(GecharlResponse::TRANSACTION_PROCESSED_STATUS["{$responseBody->status}"])) {
68
                    $this->additionalStatus = GecharlResponse::TRANSACTION_PROCESSED_STATUS["{$responseBody->status}"];
69
                }
70
            }
71
        }
72
73
        if ($this->hasError)
74
            throw new GecharlErrorException($this->message, "$code");
75
76
    }
77
78
    /**
79
     * Determine if this ise a success response object
80
     * @return bool
81
     */
82
    public function successful(): bool
83
    {
84
        return !($this->hasError);
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getTitle(): string
91
    {
92
        return $this->title;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getMessage(): string
99
    {
100
        return $this->message;
101
    }
102
103
    /**
104
     * @return object|array|null
105
     */
106
    public function getBody()
107
    {
108
        return $this->body;
109
    }
110
111
    public function __toString()
112
    {
113
        return json_encode($this->body);
114
    }
115
116
}
117