-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#184 - Client builder with two mandatory options (timeout, connect_ti…
…meout)
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\Monitoring\Check\Consumers\Client; | ||
|
||
class ClientBuilder | ||
{ | ||
|
||
private ?int $connectTimeout; | ||
|
||
private ?int $timeout; | ||
|
||
/** | ||
* @see https://docs.guzzlephp.org/en/stable/request-options.html#verify | ||
*/ | ||
private bool $verify = TRUE; | ||
|
||
private ?\Pd\Monitoring\Check\Consumers\Client\Conf\AllowRedirectsInterface $allowRedirects = NULL; | ||
|
||
|
||
private function __construct( | ||
?int $connectTimeout, | ||
?int $timeout | ||
) | ||
{ | ||
$this->connectTimeout = $connectTimeout; | ||
$this->timeout = $timeout; | ||
} | ||
|
||
|
||
public function withVerify(bool $verify): self | ||
{ | ||
$factory = clone $this; | ||
$factory->verify = $verify; | ||
|
||
return $factory; | ||
} | ||
|
||
|
||
public function withAllowRedirects(\Pd\Monitoring\Check\Consumers\Client\Conf\AllowRedirectsInterface $allowRedirects): self | ||
{ | ||
$factory = clone $this; | ||
$factory->allowRedirects = $allowRedirects; | ||
|
||
return $factory; | ||
} | ||
|
||
|
||
public function withTimeout(int $timeout): self | ||
{ | ||
$factory = clone $this; | ||
$factory->timeout = $timeout; | ||
|
||
return $factory; | ||
} | ||
|
||
|
||
public function withConnectTimeout(int $defaultTimeout): self | ||
{ | ||
$builder = clone $this; | ||
$builder->connectTimeout = $defaultTimeout; | ||
|
||
return $builder; | ||
} | ||
|
||
|
||
public static function create( | ||
int $connectTimeout, | ||
int $timeout | ||
): self | ||
{ | ||
return new self($connectTimeout, $timeout); | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, string|array<string, string>> | ||
*/ | ||
private function config(): array | ||
{ | ||
$config = [ | ||
'verify' => $this->verify, | ||
'connect_timeout' => $this->connectTimeout, | ||
'timeout' => $this->timeout, | ||
]; | ||
|
||
$headers = [ | ||
'headers' => [ | ||
'User-Agent' => 'PeckaMonitoringBot/1.0', | ||
], | ||
]; | ||
|
||
return \array_filter( | ||
\array_merge( | ||
$config, | ||
$headers, | ||
$this->allowRedirects !== NULL ? $this->allowRedirects->toArray() : [] | ||
) | ||
); | ||
} | ||
|
||
|
||
public function build(): \GuzzleHttp\Client | ||
{ | ||
return new \GuzzleHttp\Client($this->config()); | ||
} | ||
|
||
} |