Remove calls to Subscriber::setError() from ConfirmationEmailMailer
This commit is part of a task to replace Paris with Doctrine in the class ConfirmationEmailMailer. Specifically, it removes two calls to Subscriber::setError() inside ConfirmationEmailMailer::sendConfirmationEmail(). setError() was used to define an error message that was used only in one of the instances where sendConfirmationEmail() is called (API::subscribeToLists()). setError() was replaced with code that throws an exception when there is an error. Thus it was necessary to change all the places where ConfirmationEmailMailer::sendConfirmationEmail() is called to handle the exception. In some cases, there are some oddities as sendConfirmationEmail() can return false or throw an exception in case of an error and calling code must account for both. I decided to settle with this approach as refactoring the rest of this method to use exceptions instead of returning false seemed outside of the scope of this task. [MAILPOET-3815]
This commit is contained in:
@ -199,12 +199,19 @@ class Subscribers extends APIEndpoint {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : false);
|
||||
$subscriber = $this->subscribersRepository->findOneById($id);
|
||||
if ($subscriber instanceof SubscriberEntity) {
|
||||
if ($this->confirmationEmailMailer->sendConfirmationEmail($subscriber)) {
|
||||
return $this->successResponse();
|
||||
try {
|
||||
if ($this->confirmationEmailMailer->sendConfirmationEmail($subscriber)) {
|
||||
return $this->successResponse();
|
||||
} else {
|
||||
return $this->errorResponse([
|
||||
APIError::UNKNOWN => __('There was a problem with your sending method. Please check if your sending method is properly configured.', 'mailpoet'),
|
||||
]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->errorResponse([
|
||||
APIError::UNKNOWN => __('There was a problem with your sending method. Please check if your sending method is properly configured.', 'mailpoet'),
|
||||
]);
|
||||
}
|
||||
return $this->errorResponse([
|
||||
APIError::UNKNOWN => __('There was a problem with your sending method. Please check if your sending method is properly configured.', 'mailpoet'),
|
||||
]);
|
||||
} else {
|
||||
return $this->errorResponse([
|
||||
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet'),
|
||||
|
@ -148,11 +148,13 @@ class API {
|
||||
|
||||
// send confirmation email
|
||||
if ($sendConfirmationEmail) {
|
||||
$result = $this->_sendConfirmationEmail($subscriber);
|
||||
if (!$result && $subscriber->getErrors()) {
|
||||
try {
|
||||
$this->_sendConfirmationEmail($subscriber);
|
||||
} catch (\Exception $e) {
|
||||
throw new APIException(
|
||||
__(sprintf('Subscriber added to lists, but confirmation email failed to send: %s', strtolower(implode(', ', $subscriber->getErrors()))), 'mailpoet'),
|
||||
APIException::CONFIRMATION_FAILED_TO_SEND);
|
||||
__(sprintf('Subscriber added to lists, but confirmation email failed to send: %s', strtolower($e->getMessage())), 'mailpoet'),
|
||||
APIException::CONFIRMATION_FAILED_TO_SEND
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user