Issues (18)

src/Concerns/Utils.php (7 issues)

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 (config('p-connector.'.$this->profile.'.decode_response', config('p-connector.decode_response')) !== 'object') {
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...
94
            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.');
95
        }
96
97
        if (0 === $this->status || 'object' !== gettype($this->response['body'])) {
98
            return $default;
99
        }
100
101
        return _get($this->response['body'], explode('.', $attribute), $default);
102
    }
103
104
    /**
105
     * Check if the response code is equal to.
106
     *
107
     * @return bool
108
     */
109
    public function responseCodeIs(int $code)
110
    {
111
        return $this->response['status_code'] === $code;
112
    }
113
114
    /**
115
     * Check if the response code is not equal to.
116
     *
117
     * @return bool
118
     */
119
    public function responseCodeNot(int $code)
120
    {
121
        return ! $this->responseCodeIs($code);
122
    }
123
124
    /**
125
     * Check if the response code is in the given array.
126
     *
127
     * @return bool
128
     */
129
    public function responseCodeIn(array $code)
130
    {
131
        return in_array($this->response['status_code'], $code);
132
    }
133
134
    /**
135
     * Check if the response code is not in the given array.
136
     *
137
     * @return bool
138
     */
139
    public function responseCodeNotIn(array $code)
140
    {
141
        return ! $this->responseCodeIn($code);
142
    }
143
144
    /**
145
     * Check if the response code is in [200, 201, 202, 204] range.
146
     *
147
     * @return bool
148
     */
149
    public function responseOK()
150
    {
151
        return $this->responseCodeIn([200, 201, 202, 204]);
152
    }
153
154
    /**
155
     * Check if the response code is not in [200, 201, 202, 204] range.
156
     *
157
     * @return bool
158
     */
159
    public function responseNOK()
160
    {
161
        return ! $this->responseOK();
162
    }
163
164
    public function __get($name)
165
    {
166
        return $this->status ? $this->response['body']->{$name} : null;
167
    }
168
}
169