Annotate return type and use consistent return data

MAILPOET-4300
This commit is contained in:
Oluwaseun Olorunsola
2022-07-12 09:38:47 +01:00
committed by Veljko V
parent d93448c352
commit 5d8b721a52
6 changed files with 17 additions and 16 deletions

View File

@ -128,7 +128,7 @@ function AuthorizeSenderEmailModal({
makeApiRequest(senderEmailAddress)
.then((res) => {
const response = Boolean(res?.data);
const response = Boolean(res?.data?.status);
setCreateEmailApiResponse(response);
setShowLoader(response);
if (response) {

View File

@ -219,7 +219,7 @@ class Settings extends APIEndpoint {
$e->getMessage() === AuthorizedEmailsController::AUTHORIZED_EMAIL_ERROR_PENDING_CONFIRMATION
) {
// return true if the email is already authorized or pending confirmation
$response = true;
$response = ['status' => true];
} else {
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__($e->getMessage(), 'mailpoet'),

View File

@ -64,11 +64,11 @@ class AuthorizedEmailsController {
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, null);
}
public function getAllAuthorizedEmailAddress() {
public function getAllAuthorizedEmailAddress(): array {
return $this->bridge->getAuthorizedEmailAddresses(self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_ALL);
}
public function createAuthorizedEmailAddress(string $email) {
public function createAuthorizedEmailAddress(string $email): array {
$allEmails = $this->getAllAuthorizedEmailAddress();
$authorizedEmails = isset($allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_AUTHORIZED]) ? $allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_AUTHORIZED] : [];
@ -87,9 +87,8 @@ class AuthorizedEmailsController {
$finalData = $this->bridge->createAuthorizedEmailAddress($email);
if (!is_bool($finalData) && is_array($finalData)) {
$errorMessage = isset($finalData['error']) ? $finalData['error'] : ' ';
throw new \InvalidArgumentException($errorMessage);
if ($finalData && isset($finalData['error'])) {
throw new \InvalidArgumentException($finalData['error']);
}
return $finalData;

View File

@ -130,8 +130,6 @@ class Bridge {
/**
* Create Authorized Email Address
*
* returns true if done or an array of error messages
*/
public function createAuthorizedEmailAddress(string $emailAdress) {
$data = $this

View File

@ -183,11 +183,11 @@ class API {
/**
* Create Authorized Email Address
*
* returns true if done or an array of error messages
* returns ['status' => true] if done or an array of error messages ['error' => $errorBody, 'status' => false]
* @param string $emailAddress
* @return bool|array
* @return array
*/
public function createAuthorizedEmailAddress(string $emailAddress) {
public function createAuthorizedEmailAddress(string $emailAddress): array {
$body = ['email' => $emailAddress];
$result = $this->request(
$this->urlAuthorizedEmailAddresses,
@ -204,12 +204,15 @@ class API {
'error' => is_wp_error($result) ? $result->get_error_message() : $errorBody,
];
$this->loggerFactory->getLogger(LoggerFactory::TOPIC_BRIDGE)->error('CreateAuthorizedEmailAddress API call failed.', $logData);
$errorResponseData = json_decode($errorBody, true);
$fallbackError = sprintf($this->wp->__('An error has happened while performing a request, the server has responded with response code %d'), $code);
return is_array($errorResponseData) ? $errorResponseData : ['error' => $fallbackError];
$errorData = is_array($errorResponseData) && isset($errorResponseData['error']) ? $errorResponseData['error'] : $fallbackError;
return ['error' => $errorData, 'status' => false];
}
return $isSuccess;
return ['status' => $isSuccess];
}
public function setKey($apiKey) {

View File

@ -264,14 +264,15 @@ class AuthorizedEmailsControllerTest extends \MailPoetTest {
'pending' => ['pending@email.com'],
'authorized' => ['authorized@email.com']
];
$response = ['status' => true];
$bridgeMock = $this->make(Bridge::class, [
'getAuthorizedEmailAddresses' => Expected::once($array),
'createAuthorizedEmailAddress' => Expected::once(true)
'createAuthorizedEmailAddress' => Expected::once($response)
]);
$newslettersRepository = $this->diContainer->get(NewslettersRepository::class);
$controller = new AuthorizedEmailsController($this->settings, $bridgeMock, $newslettersRepository);
$result = $controller->createAuthorizedEmailAddress('new-authorized@email.com');
expect($result)->equals(true);
expect($result)->equals($response);
}
public function testItThrowsAnExceptionForReturnedArrayForCreateNewAuthorizedEmailAddress() {