Refactor mailerInstance property to mailerMethod

Naming mailerInstance was confusing since
it is not instance of Mailer class but instance of MailerMethod.
[MAILPOET-4115]
This commit is contained in:
Rostislav Wolny
2022-04-05 16:27:19 +02:00
committed by Veljko V
parent 708333e68a
commit 789385b0c4
5 changed files with 24 additions and 24 deletions

View File

@@ -53,7 +53,7 @@ class Mailer {
} }
public function getProcessingMethod() { public function getProcessingMethod() {
return ($this->mailer->mailerInstance instanceof MailPoet) ? return ($this->mailer->mailerMethod instanceof MailPoet) ?
'bulk' : 'bulk' :
'individual'; 'individual';
} }
@@ -66,7 +66,7 @@ class Mailer {
if ($this->getProcessingMethod() === 'individual') { if ($this->getProcessingMethod() === 'individual') {
throw new \LogicException('Trying to send a batch with individual processing method'); throw new \LogicException('Trying to send a batch with individual processing method');
} }
return $this->mailer->mailerInstance->send( return $this->mailer->mailerMethod->send(
$preparedNewsletters, $preparedNewsletters,
$preparedSubscribers, $preparedSubscribers,
$extraParams $extraParams
@@ -77,7 +77,7 @@ class Mailer {
if ($this->getProcessingMethod() === 'bulk') { if ($this->getProcessingMethod() === 'bulk') {
throw new \LogicException('Trying to send an individual email with a bulk processing method'); throw new \LogicException('Trying to send an individual email with a bulk processing method');
} }
return $this->mailer->mailerInstance->send( return $this->mailer->mailerMethod->send(
$preparedNewsletter, $preparedNewsletter,
$preparedSubscriber, $preparedSubscriber,
$extraParams $extraParams

View File

@@ -8,7 +8,7 @@ use MailPoet\Models\Subscriber;
class Mailer { class Mailer {
/** @var MailerMethod */ /** @var MailerMethod */
public $mailerInstance; public $mailerMethod;
const MAILER_CONFIG_SETTING_NAME = 'mta'; const MAILER_CONFIG_SETTING_NAME = 'mta';
const SENDING_LIMIT_INTERVAL_MULTIPLIER = 60; const SENDING_LIMIT_INTERVAL_MULTIPLIER = 60;
@@ -19,9 +19,9 @@ class Mailer {
const METHOD_SMTP = 'SMTP'; const METHOD_SMTP = 'SMTP';
public function __construct( public function __construct(
MailerMethod $mailerInstance MailerMethod $mailerMethod
) { ) {
$this->mailerInstance = $mailerInstance; $this->mailerMethod = $mailerMethod;
} }
public function send($newsletter, $subscriber, $extraParams = []) { public function send($newsletter, $subscriber, $extraParams = []) {
@@ -31,7 +31,7 @@ class Mailer {
$subscriber = Subscriber::findOne($subscriber->getId()); $subscriber = Subscriber::findOne($subscriber->getId());
} }
$subscriber = $this->formatSubscriberNameAndEmailAddress($subscriber); $subscriber = $this->formatSubscriberNameAndEmailAddress($subscriber);
return $this->mailerInstance->send($newsletter, $subscriber, $extraParams); return $this->mailerMethod->send($newsletter, $subscriber, $extraParams);
} }
/** /**

View File

@@ -50,7 +50,7 @@ class MailerFactory {
$returnPath = $returnPath ?? $this->getReturnPathAddress($sender); $returnPath = $returnPath ?? $this->getReturnPathAddress($sender);
switch ($mailerConfig['method']) { switch ($mailerConfig['method']) {
case Mailer::METHOD_AMAZONSES: case Mailer::METHOD_AMAZONSES:
$mailerInstance = new AmazonSES( $mailerMethod = new AmazonSES(
$mailerConfig['region'], $mailerConfig['region'],
$mailerConfig['access_key'], $mailerConfig['access_key'],
$mailerConfig['secret_key'], $mailerConfig['secret_key'],
@@ -61,7 +61,7 @@ class MailerFactory {
); );
break; break;
case Mailer::METHOD_MAILPOET: case Mailer::METHOD_MAILPOET:
$mailerInstance = new MailPoet( $mailerMethod = new MailPoet(
$mailerConfig['mailpoet_api_key'], $mailerConfig['mailpoet_api_key'],
$sender, $sender,
$replyTo, $replyTo,
@@ -70,7 +70,7 @@ class MailerFactory {
); );
break; break;
case Mailer::METHOD_SENDGRID: case Mailer::METHOD_SENDGRID:
$mailerInstance = new SendGrid( $mailerMethod = new SendGrid(
$mailerConfig['api_key'], $mailerConfig['api_key'],
$sender, $sender,
$replyTo, $replyTo,
@@ -78,7 +78,7 @@ class MailerFactory {
); );
break; break;
case Mailer::METHOD_PHPMAIL: case Mailer::METHOD_PHPMAIL:
$mailerInstance = new PHPMail( $mailerMethod = new PHPMail(
$sender, $sender,
$replyTo, $replyTo,
$returnPath, $returnPath,
@@ -86,7 +86,7 @@ class MailerFactory {
); );
break; break;
case Mailer::METHOD_SMTP: case Mailer::METHOD_SMTP:
$mailerInstance = new SMTP( $mailerMethod = new SMTP(
$mailerConfig['host'], $mailerConfig['host'],
$mailerConfig['port'], $mailerConfig['port'],
$mailerConfig['authentication'], $mailerConfig['authentication'],
@@ -102,7 +102,7 @@ class MailerFactory {
default: default:
throw new InvalidStateException(__('Mailing method does not exist.', 'mailpoet')); throw new InvalidStateException(__('Mailing method does not exist.', 'mailpoet'));
} }
return new Mailer($mailerInstance); return new Mailer($mailerMethod);
} }
private function getMailerConfig(): array { private function getMailerConfig(): array {

View File

@@ -110,7 +110,7 @@ class MailerTest extends \MailPoetTest {
public function testItCanSend() { public function testItCanSend() {
$phpMailClass = 'MailPoet\Mailer\Methods\PHPMail'; $phpMailClass = 'MailPoet\Mailer\Methods\PHPMail';
$mailerMock = Stub::makeEmpty(Mailer::class, [ $mailerMock = Stub::makeEmpty(Mailer::class, [
'mailerInstance' => Stub::make( 'mailerMethod' => Stub::make(
$phpMailClass, $phpMailClass,
['send' => Expected::exactly(1, function() { ['send' => Expected::exactly(1, function() {
return ['response' => true]; return ['response' => true];

View File

@@ -101,32 +101,32 @@ class MailerFactoryTest extends \MailPoetTest {
public function testItCanBuildCorrectMailerMethodsBasedOnConfig() { public function testItCanBuildCorrectMailerMethodsBasedOnConfig() {
$this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_PHPMAIL]); $this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_PHPMAIL]);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$this->assertInstanceOf(PHPMail::class, $mailer->mailerInstance); $this->assertInstanceOf(PHPMail::class, $mailer->mailerMethod);
$this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class)); $this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class));
$this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_AMAZONSES]); $this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_AMAZONSES]);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$this->assertInstanceOf(AmazonSES::class, $mailer->mailerInstance); $this->assertInstanceOf(AmazonSES::class, $mailer->mailerMethod);
$this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class)); $this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class));
$this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_MAILPOET]); $this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_MAILPOET]);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$this->assertInstanceOf(MailPoet::class, $mailer->mailerInstance); $this->assertInstanceOf(MailPoet::class, $mailer->mailerMethod);
$this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class)); $this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class));
$this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_SMTP]); $this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_SMTP]);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$this->assertInstanceOf(SMTP::class, $mailer->mailerInstance); $this->assertInstanceOf(SMTP::class, $mailer->mailerMethod);
$this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class)); $this->factory = new MailerFactory($this->settings, $this->diContainer->get(WPFunctions::class));
$this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_SENDGRID]); $this->settings->set('mta', $this->mtaConfigs[Mailer::METHOD_SENDGRID]);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$this->assertInstanceOf(SendGrid::class, $mailer->mailerInstance); $this->assertInstanceOf(SendGrid::class, $mailer->mailerMethod);
} }
public function testItUsesProcessedSenderDataFromSettings() { public function testItUsesProcessedSenderDataFromSettings() {
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$mailerMethod = $mailer->mailerInstance; $mailerMethod = $mailer->mailerMethod;
$this->assertInstanceOf(PHPMail::class, $mailerMethod); $this->assertInstanceOf(PHPMail::class, $mailerMethod);
expect($mailerMethod->sender)->equals([ expect($mailerMethod->sender)->equals([
'from_name' => 'Sender', 'from_name' => 'Sender',
@@ -144,7 +144,7 @@ class MailerFactoryTest extends \MailPoetTest {
public function testItUsesSenderAsReplyToWhenReplyToIsNotSet() { public function testItUsesSenderAsReplyToWhenReplyToIsNotSet() {
$this->settings->set('reply_to', null); $this->settings->set('reply_to', null);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$mailerMethod = $mailer->mailerInstance; $mailerMethod = $mailer->mailerMethod;
$this->assertInstanceOf(PHPMail::class, $mailerMethod); $this->assertInstanceOf(PHPMail::class, $mailerMethod);
expect($mailerMethod->replyTo)->equals([ expect($mailerMethod->replyTo)->equals([
'reply_to_name' => 'Sender', 'reply_to_name' => 'Sender',
@@ -156,7 +156,7 @@ class MailerFactoryTest extends \MailPoetTest {
public function testItIgnoresInvalidBounceAddressAndUsesSenderAddressInstead() { public function testItIgnoresInvalidBounceAddressAndUsesSenderAddressInstead() {
$this->settings->set('bounce.address', 'invalid'); $this->settings->set('bounce.address', 'invalid');
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$mailerMethod = $mailer->mailerInstance; $mailerMethod = $mailer->mailerMethod;
$this->assertInstanceOf(PHPMail::class, $mailerMethod); $this->assertInstanceOf(PHPMail::class, $mailerMethod);
expect($mailerMethod->returnPath)->equals('sender@email.com'); expect($mailerMethod->returnPath)->equals('sender@email.com');
} }
@@ -164,7 +164,7 @@ class MailerFactoryTest extends \MailPoetTest {
public function testItUsesSenderAddressInReplyToInCaseReplyToHasOnlyName() { public function testItUsesSenderAddressInReplyToInCaseReplyToHasOnlyName() {
$this->settings->set('reply_to', ['name' => 'Reply To']); $this->settings->set('reply_to', ['name' => 'Reply To']);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$mailerMethod = $mailer->mailerInstance; $mailerMethod = $mailer->mailerMethod;
$this->assertInstanceOf(PHPMail::class, $mailerMethod); $this->assertInstanceOf(PHPMail::class, $mailerMethod);
expect($mailerMethod->replyTo)->equals([ expect($mailerMethod->replyTo)->equals([
'reply_to_name' => 'Reply To', 'reply_to_name' => 'Reply To',
@@ -184,7 +184,7 @@ class MailerFactoryTest extends \MailPoetTest {
'address' => 'staff@mailinator.com', 'address' => 'staff@mailinator.com',
]); ]);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();
$mailerMethod = $mailer->mailerInstance; $mailerMethod = $mailer->mailerMethod;
$this->assertInstanceOf(PHPMail::class, $mailerMethod); $this->assertInstanceOf(PHPMail::class, $mailerMethod);
expect($mailerMethod->sender['from_name'])->equals(sprintf('=?utf-8?B?%s?=', base64_encode('Sender Außergewöhnlichen тест системы'))); expect($mailerMethod->sender['from_name'])->equals(sprintf('=?utf-8?B?%s?=', base64_encode('Sender Außergewöhnlichen тест системы')));
expect($mailerMethod->replyTo['reply_to_name'])->equals(sprintf('=?utf-8?B?%s?=', base64_encode('Reply-To Außergewöhnlichen тест системы'))); expect($mailerMethod->replyTo['reply_to_name'])->equals(sprintf('=?utf-8?B?%s?=', base64_encode('Reply-To Außergewöhnlichen тест системы')));