config = $config; $this->clientFactory = $clientFactory; $this->dataEncoder = $dataEncoder; } /** * Creates and configures HTTP client. * * @param string $url * @param string $method * @param array $params * @param int|null $storeId * @return ZendClient */ public function create($url, $method, array $params = [], $storeId = null): ZendClient { $apiKey = $this->getApiKey($storeId); $apiUrl = $this->buildFullApiUrl($url, $storeId); $client = $this->createNewClient(); $client->setHeaders( self::$authorizationType, sprintf('Basic %s', base64_encode($apiKey)) ); if (!empty($params)) { $encodedData = $this->dataEncoder->encode($params); $client->setRawData($encodedData, self::$jsonDataType); } $client->setMethod($method); $client->setUri($apiUrl); return $client; } /** * @return ZendClient */ private function createNewClient() { return $this->clientFactory->create(); } /** * Signifyd API key for merchant account. * * @see https://www.signifyd.com/docs/api/#/introduction/authentication * @param int|null $storeId * @return string */ private function getApiKey($storeId): string { return $this->config->getApiKey($storeId); } /** * Full URL for Singifyd API based on relative URL. * * @param string $url * @param int|null $storeId * @return string */ private function buildFullApiUrl($url, $storeId): string { $baseApiUrl = $this->getBaseApiUrl($storeId); $fullUrl = $baseApiUrl . self::$urlSeparator . ltrim($url, self::$urlSeparator); return $fullUrl; } /** * Base Sigifyd API URL without trailing slash. * * @param int|null $storeId * @return string */ private function getBaseApiUrl($storeId): string { $baseApiUrl = $this->config->getApiUrl($storeId); return rtrim($baseApiUrl, self::$urlSeparator); } }