Skip to content

Commit

Permalink
#184 - Client builder with two mandatory options (timeout, connect_ti…
Browse files Browse the repository at this point in the history
…meout)
  • Loading branch information
AloisJasa committed Feb 27, 2023
1 parent 0923607 commit 1db9f80
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
104 changes: 104 additions & 0 deletions app/Check/Consumers/Client/ClientBuilder.php
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());
}

}
29 changes: 29 additions & 0 deletions app/Check/Consumers/Client/Conf/AllowRedirects.php
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);
}

}
26 changes: 26 additions & 0 deletions app/Check/Consumers/Client/Conf/AllowRedirectsArray.php
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 app/Check/Consumers/Client/Conf/AllowRedirectsInterface.php
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;

}

0 comments on commit 1db9f80

Please sign in to comment.