Reduce the count of constants

[MAILPOET-4639]
This commit is contained in:
Jan Lysý
2023-01-10 19:50:58 +01:00
committed by Aschepikov
parent da88250fe1
commit 579de1cc86
8 changed files with 18 additions and 21 deletions

View File

@@ -70,7 +70,7 @@ class MailPoet implements MailerMethod {
case API::SENDING_STATUS_SEND_ERROR: case API::SENDING_STATUS_SEND_ERROR:
$error = $this->processSendError($result, $subscriber, $newsletter); $error = $this->processSendError($result, $subscriber, $newsletter);
return Mailer::formatMailerErrorResult($error); return Mailer::formatMailerErrorResult($error);
case API::SENDING_STATUS_OK: case API::RESPONSE_STATUS_OK:
default: default:
return Mailer::formatMailerSendSuccessResult(); return Mailer::formatMailerSendSuccessResult();
} }

View File

@@ -96,7 +96,7 @@ class AuthorizedEmailsController {
} }
$response = $this->bridge->createAuthorizedEmailAddress($email); $response = $this->bridge->createAuthorizedEmailAddress($email);
if ($response['status'] === API::AUTHORIZED_EMAIL_STATUS_ERROR) { if ($response['status'] === API::RESPONSE_STATUS_ERROR) {
throw new \InvalidArgumentException($response['message']); throw new \InvalidArgumentException($response['message']);
} }

View File

@@ -87,7 +87,7 @@ class AuthorizedSenderDomainController {
$response = $this->bridge->createAuthorizedSenderDomain($domain); $response = $this->bridge->createAuthorizedSenderDomain($domain);
if ($response['status'] === API::AUTHORIZED_DOMAIN_STATUS_ERROR) { if ($response['status'] === API::RESPONSE_STATUS_ERROR) {
throw new \InvalidArgumentException($response['message']); throw new \InvalidArgumentException($response['message']);
} }
@@ -129,7 +129,7 @@ class AuthorizedSenderDomainController {
$response = $this->bridge->verifyAuthorizedSenderDomain($domain); $response = $this->bridge->verifyAuthorizedSenderDomain($domain);
// API response contains status, but we need to check that dns array is not included // API response contains status, but we need to check that dns array is not included
if ($response['status'] === API::AUTHORIZED_DOMAIN_STATUS_ERROR && !isset($response['dns'])) { if ($response['status'] === API::RESPONSE_STATUS_ERROR && !isset($response['dns'])) {
throw new \InvalidArgumentException($response['message']); throw new \InvalidArgumentException($response['message']);
} }

View File

@@ -7,11 +7,8 @@ use MailPoet\WP\Functions as WPFunctions;
use WP_Error; use WP_Error;
class API { class API {
const AUTHORIZED_EMAIL_STATUS_OK = 'ok'; const RESPONSE_STATUS_OK = 'ok';
const AUTHORIZED_EMAIL_STATUS_ERROR = 'authorized_email_error'; const RESPONSE_STATUS_ERROR = 'error';
const AUTHORIZED_DOMAIN_STATUS_OK = 'ok';
const AUTHORIZED_DOMAIN_STATUS_ERROR = 'authorized_domain_error';
const SENDING_STATUS_OK = 'ok';
const SENDING_STATUS_CONNECTION_ERROR = 'connection_error'; const SENDING_STATUS_CONNECTION_ERROR = 'connection_error';
const SENDING_STATUS_SEND_ERROR = 'send_error'; const SENDING_STATUS_SEND_ERROR = 'send_error';
@@ -162,7 +159,7 @@ class API {
'code' => $responseCode, 'code' => $responseCode,
]; ];
} }
return ['status' => self::SENDING_STATUS_OK]; return ['status' => self::RESPONSE_STATUS_OK];
} }
public function checkBounces(array $emails) { public function checkBounces(array $emails) {
@@ -235,14 +232,14 @@ class API {
$fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $responseCode); $fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $responseCode);
return [ return [
'status' => self::AUTHORIZED_EMAIL_STATUS_ERROR, 'status' => self::RESPONSE_STATUS_ERROR,
'code' => $responseCode, 'code' => $responseCode,
'error' => is_array($errorResponseData) && isset($errorResponseData['error']) ? $errorResponseData['error'] : $fallbackError, 'error' => is_array($errorResponseData) && isset($errorResponseData['error']) ? $errorResponseData['error'] : $fallbackError,
'message' => is_array($errorResponseData) && isset($errorResponseData['error']) ? $this->getTranslatedErrorMessage($errorResponseData['error']) : $fallbackError, 'message' => is_array($errorResponseData) && isset($errorResponseData['error']) ? $this->getTranslatedErrorMessage($errorResponseData['error']) : $fallbackError,
]; ];
} }
return ['status' => self::AUTHORIZED_EMAIL_STATUS_OK]; return ['status' => self::RESPONSE_STATUS_OK];
} }
/** /**
@@ -296,7 +293,7 @@ class API {
$fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $responseCode); $fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $responseCode);
return [ return [
'status' => self::AUTHORIZED_DOMAIN_STATUS_ERROR, 'status' => self::RESPONSE_STATUS_ERROR,
'code' => $responseCode, 'code' => $responseCode,
'error' => is_array($responseBody) && isset($responseBody['error']) ? $responseBody['error'] : $fallbackError, 'error' => is_array($responseBody) && isset($responseBody['error']) ? $responseBody['error'] : $fallbackError,
'message' => is_array($responseBody) && isset($responseBody['error']) ? $this->getTranslatedErrorMessage($responseBody['error']) : $fallbackError, 'message' => is_array($responseBody) && isset($responseBody['error']) ? $this->getTranslatedErrorMessage($responseBody['error']) : $fallbackError,
@@ -308,7 +305,7 @@ class API {
return []; return [];
} }
$responseBody['status'] = self::AUTHORIZED_DOMAIN_STATUS_OK; $responseBody['status'] = self::RESPONSE_STATUS_OK;
return $responseBody; return $responseBody;
} }
@@ -332,7 +329,7 @@ class API {
if ($responseCode === 400) { if ($responseCode === 400) {
// we need to return the body as it is, but for consistency we add status and translated error message // we need to return the body as it is, but for consistency we add status and translated error message
$response = is_array($responseBody) ? $responseBody : []; $response = is_array($responseBody) ? $responseBody : [];
$response['status'] = self::AUTHORIZED_DOMAIN_STATUS_ERROR; $response['status'] = self::RESPONSE_STATUS_ERROR;
$response['message'] = $this->getTranslatedErrorMessage($response['error']); $response['message'] = $this->getTranslatedErrorMessage($response['error']);
return $response; return $response;
} }
@@ -346,7 +343,7 @@ class API {
$fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $responseCode); $fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $responseCode);
return [ return [
'status' => self::AUTHORIZED_DOMAIN_STATUS_ERROR, 'status' => self::RESPONSE_STATUS_ERROR,
'code' => $responseCode, 'code' => $responseCode,
'error' => is_array($responseBody) && isset($responseBody['error']) ? $responseBody['error'] : $fallbackError, 'error' => is_array($responseBody) && isset($responseBody['error']) ? $responseBody['error'] : $fallbackError,
'message' => is_array($responseBody) && isset($responseBody['error']) ? $this->getTranslatedErrorMessage($responseBody['error']) : $fallbackError, 'message' => is_array($responseBody) && isset($responseBody['error']) ? $this->getTranslatedErrorMessage($responseBody['error']) : $fallbackError,
@@ -358,7 +355,7 @@ class API {
return []; return [];
} }
$responseBody['status'] = self::AUTHORIZED_DOMAIN_STATUS_OK; $responseBody['status'] = self::RESPONSE_STATUS_OK;
return $responseBody; return $responseBody;
} }

View File

@@ -354,7 +354,7 @@ class AuthorizedEmailsControllerTest extends \MailPoetTest {
'createAuthorizedEmailAddress' => Expected::once([ 'createAuthorizedEmailAddress' => Expected::once([
'error' => $errorMessage, 'error' => $errorMessage,
'message' => $errorMessage, 'message' => $errorMessage,
'status' => Bridge\API::AUTHORIZED_EMAIL_STATUS_ERROR, 'status' => Bridge\API::RESPONSE_STATUS_ERROR,
]), ]),
]); ]);
$mocks = [ $mocks = [

View File

@@ -156,7 +156,7 @@ class AuthorizedSenderDomainControllerTest extends \MailPoetTest {
$verifySenderDomainsExpectation = Expected::once([ $verifySenderDomainsExpectation = Expected::once([
'error' => $errorMessage, 'error' => $errorMessage,
'message' => $errorMessage, 'message' => $errorMessage,
'status' => API::AUTHORIZED_DOMAIN_STATUS_ERROR, 'status' => API::RESPONSE_STATUS_ERROR,
]); ]);
$bridgeMock = $this->make(Bridge::class, [ $bridgeMock = $this->make(Bridge::class, [

View File

@@ -183,7 +183,7 @@ class BridgeApiTest extends \MailPoetTest {
->method('wpRemoteRetrieveBody') ->method('wpRemoteRetrieveBody')
->willReturn(json_encode(['error' => 'This domain was already added to the list.'])); ->willReturn(json_encode(['error' => 'This domain was already added to the list.']));
$result = $this->api->createAuthorizedSenderDomain('existing.com'); $result = $this->api->createAuthorizedSenderDomain('existing.com');
expect($result['status'])->equals(API::AUTHORIZED_DOMAIN_STATUS_ERROR); expect($result['status'])->equals(API::RESPONSE_STATUS_ERROR);
expect($result['error'])->equals('This domain was already added to the list.'); expect($result['error'])->equals('This domain was already added to the list.');
} }

View File

@@ -30,7 +30,7 @@ class BridgeTestMockAPI extends API {
'message' => '', 'message' => '',
], ],
], ],
'status' => API::AUTHORIZED_DOMAIN_STATUS_OK, 'status' => API::RESPONSE_STATUS_OK,
]; ];
public $apiKey; public $apiKey;