diff --git a/app/Check/Consumers/Client/ClientBuilder.php b/app/Check/Consumers/Client/ClientBuilder.php new file mode 100644 index 0000000..8d3c75c --- /dev/null +++ b/app/Check/Consumers/Client/ClientBuilder.php @@ -0,0 +1,90 @@ +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 static function create( + int $connectTimeout, + int $timeout + ): self + { + return new self($connectTimeout, $timeout); + } + + + /** + * @return array> + */ + 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() : [] + ), + static fn ($value) => ! \is_null($value) + ); + } + + + public function build(): \GuzzleHttp\Client + { + return new \GuzzleHttp\Client($this->config()); + } + +} diff --git a/tests/phpunit/Check/Consumers/Client/ClientBuilderTest.php b/tests/phpunit/Check/Consumers/Client/ClientBuilderTest.php new file mode 100644 index 0000000..d66e093 --- /dev/null +++ b/tests/phpunit/Check/Consumers/Client/ClientBuilderTest.php @@ -0,0 +1,67 @@ +build(); + + $this->assertSame(['User-Agent' => 'PeckaMonitoringBot/1.0',], $guzzle->getConfig('headers')); + $this->assertSame(TRUE, $guzzle->getConfig('verify')); + $this->assertSame(10, $guzzle->getConfig('connect_timeout')); + $this->assertSame(20, $guzzle->getConfig('timeout')); + } + + + /** + * @dataProvider dataProviderWithVerify + */ + public function testWithAll(int $timeout, int $connectionTimeout, bool $verify, array $allowRedirectsOptions): void + { + $allowRedirects = new \Pd\Monitoring\Check\Consumers\Client\Conf\AllowRedirectsArray($allowRedirectsOptions); + + $client = + \Pd\Monitoring\Check\Consumers\Client\ClientBuilder::create($connectionTimeout, $timeout) + ->withVerify($verify) + ->withAllowRedirects($allowRedirects) + ; + + $guzzle = $client->build(); + + $this->assertSame(['User-Agent' => 'PeckaMonitoringBot/1.0',], $guzzle->getConfig('headers')); + $this->assertSame($allowRedirectsOptions, $guzzle->getConfig('allow_redirects')); + $this->assertSame($verify, $guzzle->getConfig('verify')); + $this->assertSame($connectionTimeout, $guzzle->getConfig('connect_timeout')); + $this->assertSame($timeout, $guzzle->getConfig('timeout')); + } + + + public function dataProviderWithVerify(): array + { + return [ + [ + 10, + 20, + TRUE, + [ + 'max' => 5, + 'protocols' => ['http', 'https',], + 'strict' => FALSE, + 'referer' => FALSE, + 'track_redirects' => FALSE, + ], + ], + [ + 10, + 20, + FALSE, + [], + ], + ]; + } + +}