Issues (18)

src/Http/Guzzle.php (2 issues)

1
<?php
2
3
namespace MedianetDev\PConnector\Http;
4
5
use GuzzleHttp\Client;
6
use MedianetDev\PConnector\Http\Base\BaseHttp;
7
8
class Guzzle extends BaseHttp
9
{
10
    /**
11
     * @var \GuzzleHttp\Client
12
     *
13
     * The Guzzle http client instance
14
     */
15
    private $client;
16
17
    public function __construct($withAuth = true)
18
    {
19
        parent::__construct($withAuth);
20
        $this->client = new Client();
21
    }
22
23
    public function post(string $url, $data, string $profile, bool $withAuth, $headers = []): array
24
    {
25
        try {
26
            $payload = $this->prepareGuzzlePayload($profile, $withAuth, $headers);
27
            if ('array' === gettype($data)) {
28
                $payload[config('p-connector.profiles.'.$profile.'.request.post_data', config('p-connector.request.post_data', 'json'))] = $data;
29
            } else {
30
                $payload['body'] = $data;
31
            }
32
33
            return $this->parser($url, 'POST', $payload, $this->client->post($url, $payload));
34
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
35
            return $this->parser($url, 'POST', $payload, null, false, $e->getMessage());
36
        }
37
    }
38
39
    public function get(string $url, $data, string $profile, bool $withAuth, $headers = [], bool $withJson = false): array
40
    {
41
        try {
42
            $payload = $this->prepareGuzzlePayload($profile, $withAuth, $headers);
43
            if ('array' === gettype($data)) {
44
                if ($withJson) {
45
                    $payload['json'] = $data;
46
                } else {
47
                    $payload['query'] = $data;
48
                }
49
            } else {
50
                $payload['body'] = $data;
51
            }
52
53
            return $this->parser($url, 'GET', $payload, $this->client->get($url, $payload));
54
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
55
            return $this->parser($url, 'GET', $payload, null, false, $e->getMessage());
56
        }
57
    }
58
59
    public function put(string $url, $data, string $profile, bool $withAuth, $headers = []): array
60
    {
61
        try {
62
            $payload = $this->prepareGuzzlePayload($profile, $withAuth, $headers);
63
            if ('array' === gettype($data)) {
64
                $payload[config('p-connector.profiles.'.$profile.'.request.post_data', config('p-connector.request.post_data', 'json'))] = $data;
65
            } else {
66
                $payload['body'] = $data;
67
            }
68
69
            return $this->parser($url, 'PUT', $payload, $this->client->put($url, $payload));
70
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
71
            return $this->parser($url, 'PUT', $payload, null, false, $e->getMessage());
72
        }
73
    }
74
75
    public function patch(string $url, $data, string $profile, bool $withAuth, $headers = []): array
76
    {
77
        try {
78
            $payload = $this->prepareGuzzlePayload($profile, $withAuth, $headers);
79
            if ('array' === gettype($data)) {
80
                $payload[config('p-connector.profiles.'.$profile.'.request.post_data', config('p-connector.request.post_data', 'json'))] = $data;
81
            } else {
82
                $payload['body'] = $data;
83
            }
84
85
            return $this->parser($url, 'PATCH', $payload, $this->client->put($url, $payload));
86
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
87
            return $this->parser($url, 'PATCH', $payload, null, false, $e->getMessage());
88
        }
89
    }
90
91
    public function delete(string $url, $data, string $profile, bool $withAuth, $headers = []): array
92
    {
93
        try {
94
            $payload = $this->prepareGuzzlePayload($profile, $withAuth, $headers);
95
            if ('array' === gettype($data)) {
96
                $payload[config('p-connector.profiles.'.$profile.'.request.post_data', config('p-connector.request.post_data', 'json'))] = $data;
97
            } else {
98
                $payload['body'] = $data;
99
            }
100
101
            return $this->parser($url, 'DELETE', $payload, $this->client->delete($url, $payload));
102
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
103
            return $this->parser($url, 'DELETE', $payload, null, false, $e->getMessage());
104
        }
105
    }
106
107
    protected function parser(string $url, string $method, array $payload, $response, bool $status = true, ?string $errorMessage = null): array
108
    {
109
        $result['status'] = $status;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
110
        $result['request'] = [
111
            'url' => $url,
112
            'method' => $method,
113
            'payload' => $payload,
114
        ];
115
        if (! is_null($response)) {
116
            $result['response'] = [
117
                'status_code' => $response->getStatusCode(),
118
                'headers' => $response->getHeaders(),
119
                'body' => $response->getBody()->getContents(),
120
            ];
121
        } else {
122
            $result['response'] = [
123
                'status_code' => 0,
124
                'headers' => [],
125
                'body' => '{}',
126
            ];
127
        }
128
        if (! is_null($errorMessage)) {
129
            $result['error_message'] = $errorMessage;
130
        }
131
132
        return $result;
133
    }
134
135
    /**
136
     * Build the guzzle payload.
137
     *
138
     * @param  string  $profile
139
     * @param  bool  $withAuth
140
     * @param  array  $headers
141
     * @return array
142
     */
143
    protected function prepareGuzzlePayload($profile, $withAuth, $headers)
144
    {
145
        $payload['headers'] = array_merge(config(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$payload was never initialized. Although not strictly required by PHP, it is generally a good practice to add $payload = array(); before regardless.
Loading history...
146
            'p-connector.profiles.'.$profile.'.request.headers',
147
            config('p-connector.request.headers', ['Accept' => 'application/json'])
148
        ), $headers);
149
        if ($withAuth) {
150
            $payload['headers'] = array_merge($payload['headers'], $this->authManager->getAuthenticationHeader($profile));
151
        }
152
        $payload['http_errors'] = config(
153
            'p-connector.profiles.'.$profile.'.request.http_errors',
154
            config('p-connector.request.http_errors', false)
155
        );
156
        $payload['connect_timeout'] = config(
157
            'p-connector.profiles.'.$profile.'.request.connect_timeout',
158
            config('p-connector.request.connect_timeout', 3)
159
        );
160
        $payload['timeout'] = config(
161
            'p-connector.profiles.'.$profile.'.request.timeout',
162
            config('p-connector.request.timeout', 3)
163
        );
164
        $payload['verify'] = config(
165
            'p-connector.profiles.'.$profile.'.request.verify',
166
            config('p-connector.request.verify', true)
167
        );
168
169
        return $payload;
170
    }
171
}
172