Passed
Pull Request — master (#109)
by mohammad
19:24
created

Client::delete()   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
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Shetabit\Multipay\Http;
4
5
class Client implements HttpAdapter
6
{
7
    /**
8
     * guzzle http client.
9
     *
10
     * @var Client
11
     */
12
    protected $client;
13
14
    /**
15
     * Payment Driver.
16
     *
17
     * @var string
18
     */
19
    protected $driver;
20
21
    /**
22
     * Http constructor.
23
     *
24
     * @param string $baseUrl
25
     * @param string $driver
26
     */
27
    public function __construct(string $baseUrl, string $driver)
28
    {
29
        $this->driver = $driver;
30
        $this->client = new \GuzzleHttp\Client([
0 ignored issues
show
Documentation Bug introduced by
It seems like new GuzzleHttp\Client(ar...w_redirects' => false)) of type GuzzleHttp\Client is incompatible with the declared type Shetabit\Multipay\Http\Client of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
            'headers' => [
32
                'Content-Type' => 'application/json'
33
            ],
34
            'base_uri'       => $baseUrl,
35
            'http_errors' =>false,
36
            'allow_redirects'=> false,
37
        ]);
38
    }
39
40
41
    /**
42
     * Sends a GET request.
43
     *
44
     * @param string $url
45
     * @param array  $data
46
     * @param array  $headers
47
     *
48
     *
49
     * @return Response
50
     */
51
    public function get(string $url, array $data = [], array $headers = []): Response
52
    {
53
        return $this->request('GET', $url, ['query'=>$data], $headers);
54
    }
55
56
    /**
57
     * Sends a Post request.
58
     *
59
     * @param string $url
60
     * @param array  $data
61
     * @param array  $headers
62
     *
63
     *
64
     * @return Response
65
     */
66
    public function post(string $url, array $data = [], array $headers = []): Response
67
    {
68
        return $this->request('POST', $url, ['json'=>$data], $headers);
69
    }
70
71
    /**
72
     * Sends a Put request.
73
     *
74
     * @param string $url
75
     * @param array  $data
76
     * @param array  $headers
77
     *
78
     *
79
     * @return Response
80
     */
81
    public function put(string $url, array $data = [], array $headers = []): Response
82
    {
83
        return $this->request('PUT', $url, ['json'=>$data], $headers);
84
    }
85
86
    /**
87
     * Sends a Patch request.
88
     *
89
     * @param string $url
90
     * @param array  $data
91
     * @param array  $headers
92
     *
93
     *
94
     * @return Response
95
     */
96
    public function patch(string $url, array $data = [], array $headers = []): Response
97
    {
98
        return $this->request('PATCH', $url, ['json'=>$data], $headers);
99
    }
100
101
    /**
102
     * Sends a Delete request.
103
     *
104
     * @param string $url
105
     * @param array  $data
106
     * @param array  $headers
107
     *
108
     *
109
     * @return Response
110
     */
111
    public function delete(string $url, array $data = [], array $headers = []): Response
112
    {
113
        return $this->request('DELETE', $url, ['json'=>$data], $headers);
114
    }
115
116
    /**
117
     * Sends a request.
118
     *
119
     * @param string $method
120
     * @param string $url
121
     * @param array $data
122
     * @param array $headers
123
     *
124
     *
125
     * @return Response
126
     */
127
    public function request(string $method, string $url, array $data = [], array $headers = []): Response
128
    {
129
        $response = $this->client->request($method, $url, $data, $headers);
130
131
        $responseData = json_decode($response->getBody()->getContents(), true);
0 ignored issues
show
Bug introduced by
The method getBody() does not exist on Shetabit\Multipay\Http\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        $responseData = json_decode($response->/** @scrutinizer ignore-call */ getBody()->getContents(), true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
        return $this->response($responseData??[], $response->getStatusCode());
0 ignored issues
show
Bug introduced by
$response->getStatusCode() of type string is incompatible with the type integer expected by parameter $statusCode of Shetabit\Multipay\Http\Client::response(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
        return $this->response($responseData??[], /** @scrutinizer ignore-type */ $response->getStatusCode());
Loading history...
134
    }
135
136
    /**
137
     * Return Response.
138
     *
139
     * @param array $data
140
     * @param int $statusCode
141
     * @return Response
142
     */
143
    protected function response(array $data, int $statusCode): Response
144
    {
145
        $r = new Response($this->driver, $statusCode);
146
        $r->data($data);
147
148
        return $r;
149
    }
150
}
151