Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Дополняю конструктор клиента 3 версии, для возможности задания любого… #63

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/Clients/MindboxClientV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ class MindboxClientV3 extends AbstractMindboxClient
/**
* Базовый URL на который будут отправляться запросы.
*/
const BASE_V3_URL = 'https://api.mindbox.{{domainZone}}/v3/operations/';
const BASE_V3_URL = 'https://{{url}}/v3/operations/';

const TMP_V3_URL = 'https://api-ru.mindbox.cloud/v3/operations/';
/**
* Имя схемы авторизации по секретному ключу.
*/
const SECRET_KEY_AUTHORIZATION_SCHEME_NAME = 'SecretKey';

/**
* @var string Доменная зона API.
* @var string URL
*/
private $domainZone;
private $url;

/**
* @var string Уникальный идентификатор сайта/мобильного приложения/и т.п.
Expand All @@ -48,11 +47,12 @@ class MindboxClientV3 extends AbstractMindboxClient
* @param IHttpClient $httpClient Экземпляр HTTP клиента.
* @param LoggerInterface $logger Экземпляр логгера.
* @param string $domainZone
* @param string $domain
*/
public function __construct($endpointId, $secretKey, IHttpClient $httpClient, LoggerInterface $logger, $domainZone)
public function __construct($endpointId, $secretKey, IHttpClient $httpClient, LoggerInterface $logger, $domainZone, $domain = 'api.mindbox')
{
parent::__construct($secretKey, $httpClient, $logger);
$this->domainZone = $domainZone;
$this->url = $this->getApiUrl($domain, $domainZone);
$this->endpointId = $endpointId;
}

Expand Down Expand Up @@ -97,11 +97,7 @@ private function getCustomerIP()
*/
protected function prepareUrl($url, array $queryParams, $isSync = true)
{
$domain = $this->getApiUrl();

$domain = str_replace('{{domainZone}}', $this->domainZone, $domain);

return $domain . ($isSync ? 'sync' : 'async') . '?' . http_build_query($queryParams);
return $this->url . ($isSync ? 'sync' : 'async') . '?' . http_build_query($queryParams);
}

/**
Expand Down Expand Up @@ -171,19 +167,16 @@ protected function prepareResponseBody($rawBody)
}

/**
* Временное решение для старых хостингов
* Преобразование API URL
*
* @return string
*/
protected function getApiUrl()
protected function getApiUrl(string $domain, string $domainZone)
{
$return = self:: BASE_V3_URL;
switch ($this->domainZone) {
case 'api-ru':
$return = self::TMP_V3_URL;
break;
}
$domainZone = $domainZone === 'api-ru' ? 'cloud' : $domainZone;

$url = str_replace('{{url}}', $domain . '.' . $domainZone, self:: BASE_V3_URL);

return $return;
return $url;
}
}
10 changes: 8 additions & 2 deletions tests/Clients/AbstractMindboxClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ class AbstractMindboxClientTest extends TestCase
/**
* @var string
*/
protected $domain = 'ru';
protected $domainZone = 'ru';

/**
* @var string
*/
protected $domain = 'api.mindbox';

/**
* @var string
Expand Down Expand Up @@ -59,6 +64,7 @@ class AbstractMindboxClientTest extends TestCase
public function setUp(): void
{
$this->domain = $this->domain;
$this->domainZone = $this->domainZone;
$this->httpClientStub = $this->getHttpClientStub();
$this->loggerStub = $this->getLoggerStub();
$this->client = $this->getClient($this->secret, $this->httpClientStub, $this->loggerStub, $this->domain);
Expand Down Expand Up @@ -284,7 +290,7 @@ protected function getClient($secret, $httpClient, $loggerClient)
'prepareBody',
'prepareResponseBody'
])
->setConstructorArgs([$secret, $httpClient, $loggerClient, $this->domain])
->setConstructorArgs([$secret, $httpClient, $loggerClient, $this->domainZone, $this->domain])
->getMock();

$clientStub->expects($this->any())
Expand Down
2 changes: 1 addition & 1 deletion tests/Clients/MindboxClientV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function expectedArgsForSendProvider()
*/
protected function getClient($secret, $httpClient, $loggerClient)
{
$client = new MindboxClientV3($this->endpointId, $secret, $httpClient, $loggerClient, $this->domain);
$client = new MindboxClientV3($this->endpointId, $secret, $httpClient, $loggerClient, $this->domainZone, $this->domain);

return $client;
}
Expand Down
Loading