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

RonasIT / laravel-helpers / 3929967872

pending completion
3929967872

Pull #52

github

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

32 of 32 new or added lines in 3 files covered. (100.0%)

150 of 910 relevant lines covered (16.48%)

1.26 hits per line

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

44.94
/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);
8✔
27
    }
28

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

33
        return $this;
1✔
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);
6✔
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);
7✔
106

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

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

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

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

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

128
        $client = app(Client::class);
6✔
129

130
        switch ($method) {
131
            case 'get':
6✔
132
                $response = $client->get($url, $this->options);
×
133
                break;
×
134
            case 'post':
6✔
135
                $response = $client->post($url, $this->options);
×
136
                break;
×
137
            case 'put':
6✔
138
                $response = $client->put($url, $this->options);
6✔
139
                break;
6✔
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;
6✔
151
    }
152

153
    protected function logRequest(string $typeOfRequest, string $url, array $data, array $headers): void
154
    {
155
        if ($this->debug) {
7✔
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;
7✔
171

172
        if ($this->debug) {
7✔
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;
6✔
187
        $this->options['cookies'] = $this->cookies;
6✔
188
        $this->options['allow_redirects'] = $this->allowRedirects;
6✔
189
        $this->options['connect_timeout'] = $this->connectTimeout;
6✔
190

191
        return $this;
6✔
192
    }
193

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

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

203
            return;
×
204
        }
205

206
        $lowerHeaders = array_associate($headers, function ($value, $key) {
6✔
207
            return [
6✔
208
                'key' => strtolower($key),
6✔
209
                'value' => $value
6✔
210
            ];
6✔
211
        });
6✔
212

213
        $contentType = Arr::get($lowerHeaders, 'content-type');
6✔
214

215
        if (preg_match('/application\/json/', $contentType)) {
6✔
216
            $this->options['json'] = $data;
5✔
217
        } else {
218
            $this->options['form_params'] = $data;
1✔
219
        }
220
    }
221
}
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

© 2025 Coveralls, Inc