Passed
Pull Request — master (#109)
by mohammad
03:51
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 \GuzzleHttp\Client|string $baseUrl
25
     * @param string $driver
26
     */
27
    public function __construct($baseUrl, string $driver)
28
    {
29
        $this->driver = $driver;
30
        $this->client = is_string($baseUrl)
0 ignored issues
show
Documentation Bug introduced by
It seems like is_string($baseUrl) ? ne...' => false)) : $baseUrl 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
            ? new \GuzzleHttp\Client([
32
                'headers' => [
33
                    'Content-Type' => 'application/json'
34
                ],
35
                'base_uri'       => $baseUrl,
36
                'http_errors' =>false,
37
                'allow_redirects'=> false,
38
            ])
39
            : $baseUrl;
40
    }
41
42
43
    /**
44
     * Sends a GET request.
45
     *
46
     * @param string $url
47
     * @param array  $data
48
     * @param array  $headers
49
     *
50
     *
51
     * @return Response
52
     */
53
    public function get(string $url, array $data = [], array $headers = []): Response
54
    {
55
        return $this->request('GET', $url, ['query'=>$data], $headers);
56
    }
57
58
    /**
59
     * Sends a Post request.
60
     *
61
     * @param string $url
62
     * @param array  $data
63
     * @param array  $headers
64
     *
65
     *
66
     * @return Response
67
     */
68
    public function post(string $url, array $data = [], array $headers = []): Response
69
    {
70
        return $this->request('POST', $url, ['json'=>$data], $headers);
71
    }
72
73
    /**
74
     * Sends a Put request.
75
     *
76
     * @param string $url
77
     * @param array  $data
78
     * @param array  $headers
79
     *
80
     *
81
     * @return Response
82
     */
83
    public function put(string $url, array $data = [], array $headers = []): Response
84
    {
85
        return $this->request('PUT', $url, ['json'=>$data], $headers);
86
    }
87
88
    /**
89
     * Sends a Patch request.
90
     *
91
     * @param string $url
92
     * @param array  $data
93
     * @param array  $headers
94
     *
95
     *
96
     * @return Response
97
     */
98
    public function patch(string $url, array $data = [], array $headers = []): Response
99
    {
100
        return $this->request('PATCH', $url, ['json'=>$data], $headers);
101
    }
102
103
    /**
104
     * Sends a Delete request.
105
     *
106
     * @param string $url
107
     * @param array  $data
108
     * @param array  $headers
109
     *
110
     *
111
     * @return Response
112
     */
113
    public function delete(string $url, array $data = [], array $headers = []): Response
114
    {
115
        return $this->request('DELETE', $url, ['json'=>$data], $headers);
116
    }
117
118
    /**
119
     * Sends a request.
120
     *
121
     * @param string $method
122
     * @param string $url
123
     * @param array $data
124
     * @param array $headers
125
     *
126
     *
127
     * @return Response
128
     */
129
    public function request(string $method, string $url, array $data = [], array $headers = []): Response
130
    {
131
        $response = $this->client->request($method, $url, $data, $headers);
132
133
        $responseData = json_decode(($response->getBody() ?? new \stdClass())->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

133
        $responseData = json_decode(($response->/** @scrutinizer ignore-call */ getBody() ?? new \stdClass())->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...
Bug introduced by
The method getContents() does not exist on stdClass. ( Ignorable by Annotation )

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

133
        $responseData = json_decode(($response->getBody() ?? new \stdClass())->/** @scrutinizer ignore-call */ 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...
134
135
        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

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