Skip to content

Commit 8a7b50a

Browse files
committed
Refactors
1 parent 58bfb80 commit 8a7b50a

File tree

2 files changed

+63
-63
lines changed

2 files changed

+63
-63
lines changed

src/Console/Commands/CurlCommand.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ public function handle()
2626
private function gatherOptions()
2727
{
2828
return [
29-
'method' => $this->option('get') ? 'GET' : $this->option('request'),
30-
'url' => $this->argument('url'),
31-
'headers' => $this->option('header'),
29+
'basic' => $this->option('basic'),
30+
'cert' => $this->option('cert'),
31+
'compressed' => $this->option('compressed'),
32+
'connectTimeout' => $this->option('connect-timeout'),
3233
'data' => $this->option('data'),
33-
'rawData' => $this->option('data-raw'),
3434
'dataUrlEncode' => $this->option('data-urlencode'),
35-
'fields' => $this->option('form'),
3635
'digest' => $this->option('digest'),
37-
'basic' => $this->option('basic'),
38-
'connectTimeout' => $this->option('connect-timeout'),
36+
'fields' => $this->option('form'),
37+
'headers' => $this->option('header'),
38+
'insecure' => $this->option('insecure'),
39+
'key' => $this->option('key'),
3940
'maxTimeout' => $this->option('max-timeout'),
41+
'method' => $this->option('get') ? 'GET' : $this->option('request'),
42+
'rawData' => $this->option('data-raw'),
4043
'retry' => $this->option('retry'),
4144
'silent' => $this->option('curl-silent'),
45+
'url' => $this->argument('url'),
4246
'user' => $this->option('user'),
43-
'compressed' => $this->option('compressed'),
44-
'insecure' => $this->option('insecure'),
45-
'cert' => $this->option('cert'),
46-
'key' => $this->option('key'),
4747
];
4848
}
4949
}

src/Models/Request.php

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77

88
class Request
99
{
10-
private string $url;
10+
private ?int $connectTimeout = null;
1111

12-
private string $method;
12+
private array $data = [];
1313

1414
private array $headers = [];
1515

16-
private array $data = [];
16+
private bool $insecure = false;
1717

18-
private bool $rawData = false;
18+
private ?int $maxTimeout = null;
19+
20+
private string $method;
1921

2022
private bool $multipartFormData = false;
2123

22-
private ?string $username = null;
24+
private array $guzzleOptions = [];
2325

2426
private ?string $password = null;
2527

26-
private ?int $maxTimeout = null;
27-
28-
private ?int $connectTimeout = null;
28+
private bool $rawData = false;
2929

30-
private array $options = [];
30+
private string $url;
3131

32-
private bool $insecure = false;
32+
private ?string $username = null;
3333

3434
private function __construct($url, $method)
3535
{
@@ -114,14 +114,14 @@ public static function create(array $data): self
114114
@[$certificate, $password] = explode(':', $data['cert'], 2);
115115

116116
if (isset($password)) {
117-
$request->options['cert'] = [$certificate, $password];
117+
$request->guzzleOptions['cert'] = [$certificate, $password];
118118
} else {
119-
$request->options['cert'] = $certificate;
119+
$request->guzzleOptions['cert'] = $certificate;
120120
}
121121
}
122122

123123
if (isset($data['key'])) {
124-
$request->options['ssl_key'] = $data['key'];
124+
$request->guzzleOptions['ssl_key'] = $data['key'];
125125
}
126126

127127
if ($data['insecure']) {
@@ -131,96 +131,79 @@ public static function create(array $data): self
131131
return $request;
132132
}
133133

134+
public function connectTimeout(): int
135+
{
136+
return $this->connectTimeout;
137+
}
138+
134139
public function data(): array
135140
{
136141
return $this->data;
137142
}
138143

139-
public function hasUsernameOrPassword(): bool
144+
public function hasConnectTimeout(): bool
140145
{
141-
return isset($this->username) || isset($this->password);
146+
return isset($this->connectTimeout);
142147
}
143148

144149
public function hasMaxTimeout(): bool
145150
{
146151
return isset($this->maxTimeout);
147152
}
148153

149-
public function hasConnectTimeout(): bool
154+
public function hasUsernameOrPassword(): bool
150155
{
151-
return isset($this->connectTimeout);
156+
return isset($this->username) || isset($this->password);
152157
}
153158

154159
public function headers(): array
155160
{
156161
return $this->headers;
157162
}
158163

159-
public function isRawData(): bool
160-
{
161-
return $this->rawData;
162-
}
163-
164-
public function method(): string
165-
{
166-
return $this->method;
167-
}
168-
169-
public function url(): string
164+
public function isInsecure(): bool
170165
{
171-
return $this->url;
166+
return $this->insecure;
172167
}
173168

174169
public function isMultipartFormData(): bool
175170
{
176171
return $this->multipartFormData;
177172
}
178173

179-
public function username(): string
180-
{
181-
return $this->username ?? '';
182-
}
183-
184-
public function password(): string
174+
public function isRawData(): bool
185175
{
186-
return $this->password ?? '';
176+
return $this->rawData;
187177
}
188178

189179
public function maxTimeout(): int
190180
{
191181
return $this->maxTimeout;
192182
}
193183

194-
public function connectTimeout(): int
184+
public function method(): string
195185
{
196-
return $this->connectTimeout;
186+
return $this->method;
197187
}
198188

199189
public function options(): array
200190
{
201-
return $this->options;
191+
return $this->guzzleOptions;
202192
}
203193

204-
public function isInsecure(): bool
194+
public function password(): string
205195
{
206-
return $this->insecure;
196+
return $this->password ?? '';
207197
}
208198

209-
private static function convertDataType(string $value)
199+
public function url(): string
210200
{
211-
return preg_match('/^[1-9]\d*$/', $value) ? intval($value) : $value;
201+
return $this->url;
212202
}
213203

214-
private static function parseData(array $data): array
204+
public function username(): string
215205
{
216-
parse_str(implode('&', $data), $data);
217-
array_walk_recursive($data, function (&$value) {
218-
if (is_string($value)) {
219-
$value = self::convertDataType($value);
220-
}
221-
});
222-
223-
return $data;
206+
return $this->username ?? '';
224207
}
225208

226209
private static function buildUrl(array $url): string
@@ -237,4 +220,21 @@ private static function buildUrl(array $url): string
237220

238221
return $output;
239222
}
223+
224+
private static function convertDataType(string $value)
225+
{
226+
return preg_match('/^[1-9]\d*$/', $value) ? intval($value) : $value;
227+
}
228+
229+
private static function parseData(array $data): array
230+
{
231+
parse_str(implode('&', $data), $data);
232+
array_walk_recursive($data, function (&$value) {
233+
if (is_string($value)) {
234+
$value = self::convertDataType($value);
235+
}
236+
});
237+
238+
return $data;
239+
}
240240
}

0 commit comments

Comments
 (0)