Passed
Push — master ( 5ceae2...aa22fb )
by Soufiene
09:17 queued 06:12
created

Utils::responseCodeIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace MedianetDev\PConnector\Concerns;
4
5
trait Utils
6
{
7
    /**
8
     * Load the response from http client to PConnector.
9
     */
10
    private function loadResponse($data)
11
    {
12
        $this->status = $data['status'];
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
13
        $this->request = $data['request'];
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
14
        $this->errorMessage = $this->status ? null : $data['error_message'];
0 ignored issues
show
Bug Best Practice introduced by
The property errorMessage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
16
        $this->response = $data['response'];
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        switch ($this->decodeResponse) {
0 ignored issues
show
Bug Best Practice introduced by
The property decodeResponse does not exist on MedianetDev\PConnector\Concerns\Utils. Since you implemented __get, consider adding a @property annotation.
Loading history...
18
            case 'object':
19
                $this->response['body'] = json_decode($data['response']['body']);
20
                break;
21
            case 'array':
22
                $this->response['body'] = json_decode($data['response']['body'], true);
23
                break;
24
            default:
25
                break;
26
        }
27
    }
28
29
    /**
30
     * Log the request & response data to the log files.
31
     *
32
     * @return void
33
     */
34
    public function log()
35
    {
36
        app('log')->debug(
37
            '[PConnector: '.$this->profile.'] '.
0 ignored issues
show
Bug Best Practice introduced by
The property profile does not exist on MedianetDev\PConnector\Concerns\Utils. Since you implemented __get, consider adding a @property annotation.
Loading history...
38
            "\n------------------- gateway request --------------------".
39
            "\n#Url: ".$this->request['url'].
40
            "\n#Method: ".$this->request['method'].
41
            "\n#Data: ".json_encode($this->request['payload']).
42
            "\n------------------- gateway response -------------------".
43
            "\n#Status code: ".$this->response['status_code'].
44
            "\n#Headers: ".json_encode($this->response['headers']).
45
            "\n#Body: ".(is_string($this->response['body']) ? $this->response['body'] : json_encode($this->response['body'])).
46
            "\n#Error: ".($this->errorMessage ? $this->errorMessage : 'none').
47
            "\n--------------------------------------------------------"
48
        );
49
    }
50
51
    /**
52
     * Log the request & response data to the log files if response code is equal to.
53
     *
54
     * @param  int  $responseCode
55
     * @return void
56
     */
57
    public function logIfResponseCodeNot($responseCode)
58
    {
59
        if ($this->responseCodeNot($responseCode)) {
60
            $this->log();
61
        }
62
    }
63
64
    /**
65
     * Dump the \MedianetDev\PConnector\PConnector using laravel dump function.
66
     *
67
     * @return \MedianetDev\PConnector\PConnector
68
     */
69
    public function dump()
70
    {
71
        dump($this);
72
73
        return $this;
74
    }
75
76
    /**
77
     * Dump and die the \MedianetDev\PConnector\PConnector using laravel dd function.
78
     */
79
    public function dd()
80
    {
81
        dd($this);
82
    }
83
84
    /**
85
     * Get attribute from the response.
86
     *
87
     * @param  string  $attribute  The attribute path, for nested attributes use the "." separator [EX: "profile.name"]
88
     * @param  mixed  $default  The fallback value if the attribute is not on the response object
89
     * @return mixed
90
     */
91
    public function getAttribute($attribute, $default = null)
92
    {
93
        if (0 === $this->status) {
94
            return $default;
95
        }
96
        if ('object' !== gettype($this->response['body'])) {
97
            throw new \BadMethodCallException('You can use the get() function only if you are parsing the response as object, your response body type is: '.gettype($this->response['body']).'. You can set in the config with the "decode_response" key.');
98
        }
99
100
        return _get($this->response['body'], explode('.', $attribute), $default);
101
    }
102
103
    /**
104
     * Check if the response code is equal to.
105
     *
106
     * @return bool
107
     */
108
    public function responseCodeIs(int $code)
109
    {
110
        return $this->response['status_code'] === $code;
111
    }
112
113
    /**
114
     * Check if the response code is not equal to.
115
     *
116
     * @return bool
117
     */
118
    public function responseCodeNot(int $code)
119
    {
120
        return ! $this->responseCodeNot($code);
121
    }
122
123
    /**
124
     * Check if the response code is in the given array.
125
     *
126
     * @return bool
127
     */
128
    public function responseCodeIn(array $code)
129
    {
130
        return in_array($this->response['status_code'], $code);
131
    }
132
133
    /**
134
     * Check if the response code is not in the given array.
135
     *
136
     * @return bool
137
     */
138
    public function responseCodeNotIn(array $code)
139
    {
140
        return ! $this->responseCodeIn($code);
141
    }
142
143
    /**
144
     * Check if the response code is in [200, 201, 202, 204] range.
145
     *
146
     * @return bool
147
     */
148
    public function responseOK()
149
    {
150
        return $this->responseCodeIn([200, 201, 202, 204]);
151
    }
152
153
    /**
154
     * Check if the response code is not in [200, 201, 202, 204] range.
155
     *
156
     * @return bool
157
     */
158
    public function responseNOK()
159
    {
160
        return ! $this->responseOK();
161
    }
162
163
    public function __get($name)
164
    {
165
        return $this->status ? $this->response['body']->{$name} : null;
166
    }
167
}
168