Skip to content

Commit afd0877

Browse files
committed
Add connect-timeout and timeout parameters to check method on UrlChecker class
1 parent 88784ab commit afd0877

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ Here you can see how to use it 👇
4444
```php
4545
use Lemaur\UrlChecker\UrlChecker;
4646

47-
$userAgent = 'MyApp/1.0 (UrlChecker)';
48-
49-
$response = UrlChecker::check('https://google.com', $userAgent);
47+
$response = UrlChecker::check(
48+
url: 'https://google.com',
49+
userAgent: 'MyApp/1.0 (UrlChecker)',
50+
connectTimeout: 5,
51+
timeout: 10,
52+
);
5053
// \Lemaur\UrlChecker\DataTransferObject\CheckData
5154

5255
$response->statusCode;

src/UrlChecker.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public static function fake(array $queue): void
2929
self::$queue = $queue;
3030
}
3131

32-
public static function check(string $url, ?string $userAgent = null): CheckData
32+
public static function check(string $url, ?string $userAgent = null, ?int $connectTimeout = null, ?int $timeout = null): CheckData
3333
{
3434
try {
35-
$response = (new self())->getResponse($url, $userAgent);
35+
$response = (new self())->getResponse($url, $userAgent, $connectTimeout, $timeout);
3636

3737
if (!$response instanceof ResponseInterface) {
3838
return new CheckData(
@@ -81,16 +81,16 @@ private function getConfig(): array
8181
/**
8282
* @throws GuzzleException
8383
*/
84-
private function getResponse(string $url, ?string $userAgent = null): ?ResponseInterface
84+
private function getResponse(string $url, ?string $userAgent = null, ?int $connectTimeout = null, ?int $timeout = null): ?ResponseInterface
8585
{
8686
$response = null;
8787

8888
$client = (new Client($this->getConfig()));
8989

9090
try {
9191
$client->get($url, [
92-
'connect_timeout' => 2,
93-
'timeout' => 5,
92+
'connect_timeout' => $connectTimeout ?? 2,
93+
'timeout' => $timeout ?? 5,
9494
'headers' => [
9595
'User-Agent' => implode(' ', array_filter([
9696
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',

0 commit comments

Comments
 (0)