VtuDotNGResponse   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 5
A successful() 0 4 1
A getCode() 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-vtung
6
 * Company: Stimolive Technologies Limited
7
 * Class Name: VtuDotNGResponse.php
8
 * Date Created: 9/27/20
9
 * Time Created: 6:00 PM
10
 */
11
12
namespace HenryEjemuta\LaravelVtuDotNG\Classes;
13
14
15
use HenryEjemuta\LaravelVtuDotNG\Exceptions\VtuDotNGErrorException;
16
17
class VtuDotNGResponse
18
{
19
    private $message;
20
21
    /**
22
     * @var bool
23
     */
24
    private $hasError;
25
26
    /**
27
     * @var string
28
     */
29
    private $code;
30
31
    /**
32
     * Response Body from
33
     * @var object|null $body
34
     */
35
    private $body;
36
37
38
    /**
39
     * VtuDotNGResponse constructor.
40
     * @param string $code
41
     * @param string $message
42
     * @param object|array|null $responseBody
43
     * @throws VtuDotNGErrorException
44
     */
45
    public function __construct(string $code = 'failed', $message = 'Unable to reach VTU.ng Server', $responseBody = null)
46
    {
47
        if($message == 'Your account is not activated for API access. Kindly upgrade to a Reseller Account to access our API'){
48
            $code = "upgrade";
49
        }
50
        $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...
51
        $this->code = strtolower("$code");
52
        $this->message = $message;
53
        $this->hasError = !in_array($this->code, ["success", "failure"]);
54
55
        if ($this->hasError) {
56
            $statusCode = (($this->code == "failed") ? 503 : (($this->code == "upgrade") ? 401 : 422));
57
            throw new VtuDotNGErrorException($message, $statusCode);
58
        }
59
60
    }
61
62
    /**
63
     * Determine if this ise a success response object
64
     * @return bool
65
     */
66
    public function successful(): bool
67
    {
68
        return !($this->hasError);
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getCode(): string
75
    {
76
        return $this->code;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getMessage(): string
83
    {
84
        return $this->message;
85
    }
86
87
    /**
88
     * @return object|array|null
89
     */
90
    public function getBody()
91
    {
92
        return $this->body;
93
    }
94
95
    public function __toString()
96
    {
97
        return json_encode($this->body);
98
    }
99
100
}
101