Skip to content

Commit

Permalink
Add connect-timeout and timeout parameters to check method on `…
Browse files Browse the repository at this point in the history
…UrlChecker` class
  • Loading branch information
leMaur committed Mar 24, 2024
1 parent 88784ab commit afd0877
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ Here you can see how to use it 👇
```php
use Lemaur\UrlChecker\UrlChecker;

$userAgent = 'MyApp/1.0 (UrlChecker)';

$response = UrlChecker::check('https://google.com', $userAgent);
$response = UrlChecker::check(
url: 'https://google.com',
userAgent: 'MyApp/1.0 (UrlChecker)',
connectTimeout: 5,
timeout: 10,
);
// \Lemaur\UrlChecker\DataTransferObject\CheckData

$response->statusCode;
Expand Down
10 changes: 5 additions & 5 deletions src/UrlChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public static function fake(array $queue): void
self::$queue = $queue;
}

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

if (!$response instanceof ResponseInterface) {
return new CheckData(
Expand Down Expand Up @@ -81,16 +81,16 @@ private function getConfig(): array
/**
* @throws GuzzleException
*/
private function getResponse(string $url, ?string $userAgent = null): ?ResponseInterface
private function getResponse(string $url, ?string $userAgent = null, ?int $connectTimeout = null, ?int $timeout = null): ?ResponseInterface
{
$response = null;

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

try {
$client->get($url, [
'connect_timeout' => 2,
'timeout' => 5,
'connect_timeout' => $connectTimeout ?? 2,
'timeout' => $timeout ?? 5,
'headers' => [
'User-Agent' => implode(' ', array_filter([
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
Expand Down

0 comments on commit afd0877

Please sign in to comment.