-
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
4 changed files
with
171 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,104 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\Monitoring\Check\Consumers\Client; | ||
|
||
class ClientBuilder | ||
{ | ||
|
||
private ?int $connectTimeout; | ||
|
||
private ?int $timeout; | ||
|
||
private bool $verify; | ||
|
||
private ?\Pd\Monitoring\Check\Consumers\Client\Conf\AllowRedirectsInterface $allowRedirects; | ||
|
||
|
||
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->toArray() | ||
) | ||
); | ||
} | ||
|
||
|
||
public function build(): \GuzzleHttp\Client | ||
{ | ||
return new \GuzzleHttp\Client($this->config()); | ||
} | ||
|
||
} |
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,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\Monitoring\Check\Consumers\Client\Conf; | ||
|
||
class AllowRedirects implements AllowRedirectsInterface | ||
{ | ||
|
||
private bool $isAllowed; | ||
|
||
|
||
private function __construct(bool $isAllowed) | ||
{ | ||
$this->isAllowed = $isAllowed; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
AllowRedirectsInterface::ALLOW_REDIRECTS => $this->isAllowed, | ||
]; | ||
} | ||
|
||
|
||
public static function create(bool $isAllowed): self | ||
{ | ||
return new self($isAllowed); | ||
} | ||
|
||
} |
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,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\Monitoring\Check\Consumers\Client\Conf; | ||
|
||
class AllowRedirectsArray implements AllowRedirectsInterface | ||
{ | ||
|
||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
private array $conf; | ||
|
||
|
||
public function __construct(array $conf) | ||
{ | ||
$this->conf = $conf; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
AllowRedirectsInterface::ALLOW_REDIRECTS => $this->conf, | ||
]; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
app/Check/Consumers/Client/Conf/AllowRedirectsInterface.php
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,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\Monitoring\Check\Consumers\Client\Conf; | ||
|
||
interface AllowRedirectsInterface | ||
{ | ||
|
||
public const ALLOW_REDIRECTS = 'allow_redirects'; | ||
|
||
public function toArray(): array; | ||
|
||
} |