*/ final class HttpClientConfigurator { /** * @var string */ private $endpoint = 'https://api.mailgun.net'; /** * If debug is true we will send all the request to the endpoint without appending any path. * * @var bool */ private $debug = false; /** * @var string */ private $apiKey; /** * @var UriFactoryInterface */ private $uriFactory; /** * @var ClientInterface */ private $httpClient; /** * @var History */ private $responseHistory; public function __construct() { $this->responseHistory = new History(); } public function createConfiguredClient(): PluginClient { $plugins = [ new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->endpoint)), new Plugin\HeaderDefaultsPlugin([ 'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)', 'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())), ]), new Plugin\HistoryPlugin($this->responseHistory), ]; if ($this->debug) { $plugins[] = new ReplaceUriPlugin($this->getUriFactory()->createUri($this->endpoint)); } return new PluginClient($this->getHttpClient(), $plugins); } public function setDebug(bool $debug): self { $this->debug = $debug; return $this; } public function setEndpoint(string $endpoint): self { $this->endpoint = $endpoint; return $this; } public function getApiKey(): string { return $this->apiKey; } public function setApiKey(string $apiKey): self { $this->apiKey = $apiKey; return $this; } private function getUriFactory(): UriFactoryInterface { if (null === $this->uriFactory) { $this->uriFactory = Psr17FactoryDiscovery::findUrlFactory(); } return $this->uriFactory; } public function setUriFactory(UriFactoryInterface $uriFactory): self { $this->uriFactory = $uriFactory; return $this; } private function getHttpClient(): ClientInterface { if (null === $this->httpClient) { $this->httpClient = Psr18ClientDiscovery::find(); } return $this->httpClient; } public function setHttpClient(ClientInterface $httpClient): self { $this->httpClient = $httpClient; return $this; } public function getResponseHistory(): History { return $this->responseHistory; } }