• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 3899247356

pending completion
3899247356

Pull #52

github

GitHub
Merge b72160a65 into 71346419b
Pull Request #52: feat: update http request service to use easy mock;

22 of 22 new or added lines in 2 files covered. (100.0%)

56 of 936 relevant lines covered (5.98%)

0.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.7
/src/Services/HttpRequestService.php
1
<?php
2

3
namespace RonasIT\Support\Services;
4

5
use GuzzleHttp\Client;
6
use Illuminate\Support\Arr;
7
use GuzzleHttp\Cookie\CookieJar;
8
use Psr\Http\Message\ResponseInterface;
9
use GuzzleHttp\Exception\RequestException;
10
use RonasIT\Support\Exceptions\UnknownRequestMethodException;
11

12
class HttpRequestService
13
{
14
    protected $debug;
15

16
    protected $connectTimeout = 0;
17
    protected $allowRedirects = true;
18

19
    protected $options = [];
20
    protected $cookies = null;
21

22
    protected $response;
23

24
    public function __construct()
25
    {
26
        $this->debug = config('defaults.http_service_debug', false);
1✔
27
    }
28

29
    public function set(string $key, $value): self
30
    {
31
        $this->options[$key] = $value;
×
32

33
        return $this;
×
34
    }
35

36
    public function json(): array
37
    {
38
        $stringResponse = (string) $this->response->getBody();
×
39

40
        return json_decode($stringResponse, true);
×
41
    }
42

43
    public function getResponse(): ResponseInterface
44
    {
45
        return $this->response;
×
46
    }
47

48
    public function saveCookieSession(): self
49
    {
50
        $this->cookies = app(CookieJar::class);
×
51

52
        return $this;
×
53
    }
54

55
    public function getCookie(): array
56
    {
57
        if (empty($this->cookies)) {
×
58
            return [];
×
59
        }
60

61
        return $this->cookies->toArray();
×
62
    }
63

64
    public function allowRedirects(bool $value = true): self
65
    {
66
        $this->allowRedirects = $value;
×
67

68
        return $this;
×
69
    }
70

71
    public function setConnectTimeout(int $seconds = 0): self
72
    {
73
        $this->connectTimeout = $seconds;
×
74

75
        return $this;
×
76
    }
77

78
    public function get(string $url, array $data = [], array $headers = []): self
79
    {
80
        return $this->send('get', $url, $data, $headers);
1✔
81
    }
82

83
    public function post(string $url, array $data, array $headers = []): self
84
    {
85
        return $this->send('post', $url, $data, $headers);
×
86
    }
87

88
    public function delete(string $url, array $headers = []): self
89
    {
90
        return $this->send('delete', $url, [], $headers);
×
91
    }
92

93
    public function put(string $url, array $data, array $headers = []): self
94
    {
95
        return $this->send('put', $url, $data, $headers);
×
96
    }
97

98
    public function patch(string $url, array $data, array $headers = []): self
99
    {
100
        return $this->send('patch', $url, $data, $headers);
×
101
    }
102

103
    protected function send(string $method, string $url, array $data = [], array $headers = []): self
104
    {
105
        $startTime = microtime(true);
1✔
106

107
        $this->logRequest($method, $url, $data, $headers);
1✔
108

109
        try {
110
            $this->response = $this->sendRequest($method, $url, $data, $headers);
1✔
111

112
            return $this;
1✔
113
        } catch (RequestException $exception) {
×
114
            $this->response = $exception->getResponse();
×
115

116
            throw $exception;
×
117
        } finally {
118
            $this->logResponse($startTime);
1✔
119
            $this->options = [];
1✔
120
        }
121
    }
122

123
    protected function sendRequest($method, $url, array $data = [], array $headers = []): ResponseInterface
124
    {
125
        $this->setOptions($headers);
×
126
        $this->setData($method, $headers, $data);
×
127

128
        $client = new Client();
×
129

130
        switch ($method) {
131
            case 'get':
×
132
                $response = $client->get($url, $this->options);
×
133
                break;
×
134
            case 'post':
×
135
                $response = $client->post($url, $this->options);
×
136
                break;
×
137
            case 'put':
×
138
                $response = $client->put($url, $this->options);
×
139
                break;
×
140
            case 'patch':
×
141
                $response = $client->patch($url, $this->options);
×
142
                break;
×
143
            case 'delete':
×
144
                $response = $client->delete($url, $this->options);
×
145
                break;
×
146
            default :
147
                throw app(UnknownRequestMethodException::class)->setMethod($method);
×
148
        }
149

150
        return $response;
×
151
    }
152

153
    protected function logRequest(string $typeOfRequest, string $url, array $data, array $headers): void
154
    {
155
        if ($this->debug) {
1✔
156
            logger('');
×
157
            logger('-------------------------------------');
×
158
            logger('');
×
159
            logger("sending {$typeOfRequest} request:", [
×
160
                'url' => $url,
×
161
                'data' => $data,
×
162
                'headers' => $headers
×
163
            ]);
×
164
            logger('');
×
165
        }
166
    }
167

168
    protected function logResponse(?int $time = null): void
169
    {
170
        $endTime = (empty($time)) ? null : microtime(true) - $time;
1✔
171

172
        if ($this->debug) {
1✔
173
            logger('');
×
174
            logger('-------------------------------------');
×
175
            logger('');
×
176
            logger('getting response: ');
×
177
            logger('code', ["<{$this->response->getStatusCode()}>"]);
×
178
            logger('body', ["<{$this->response->getBody()}>"]);
×
179
            logger('time', [$endTime]);
×
180
            logger('');
×
181
        }
182
    }
183

184
    private function setOptions(array $headers): self
185
    {
186
        $this->options['headers'] = $headers;
×
187
        $this->options['cookies'] = $this->cookies;
×
188
        $this->options['allow_redirects'] = $this->allowRedirects;
×
189
        $this->options['connect_timeout'] = $this->connectTimeout;
×
190

191
        return $this;
×
192
    }
193

194
    private function setData(string $method, array $headers, array $data = []): void
195
    {
196
        if (empty($data)) {
×
197
            return;
×
198
        }
199

200
        if ($method == 'get') {
×
201
            $this->options['query'] = $data;
×
202

203
            return;
×
204
        }
205

206
        $contentType = elseChain(
×
207
            function () use ($headers) {
×
208
                return Arr::get($headers, 'Content-Type');
×
209
            },
×
210
            function () use ($headers) {
×
211
                return Arr::get($headers, 'content-type');
×
212
            },
×
213
            function () use ($headers) {
×
214
                return Arr::get($headers, 'CONTENT-TYPE');
×
215
            }
×
216
        );
×
217

218
        if (preg_match('/application\/json/', $contentType)) {
×
219
            $this->options['json'] = $data;
×
220

221
            return;
×
222
        }
223

224
        $this->options['form_params'] = $data;
×
225
    }
226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc