Skip to content

Commit

Permalink
Merged PR 58003: Use constants for queue topic names
Browse files Browse the repository at this point in the history
## What's being changed

This PR introduces class constants for all topic names. I've placed the constants in the same class as the `publish()` call (if they are only used in that one place) or else in a related model class.

## Why it's being changed

This is designed to protect us against error when updating queue topic names in future.

## How to review / test this change

- Test all the topic names, see each one output into the queue table (for subscription, pick one trigger that will publish a queue message)
- Confirm that we only use constants when calling `PublisherInterface::publish()`

Related work items: #265572
  • Loading branch information
sta1r committed Sep 23, 2024
1 parent 0c76e89 commit 10ea75a
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Controller/Email/Accountcallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dotdigitalgroup\Email\Helper\Data;
use Dotdigitalgroup\Email\Model\Integration\IntegrationSetup;
use Dotdigitalgroup\Email\Model\Integration\IntegrationSetupFactory;
use Dotdigitalgroup\Email\Model\Sync\Integration\IntegrationInsights;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Action\Context;
Expand Down Expand Up @@ -118,7 +119,7 @@ public function execute()
]);

$this->helper->log('----PUBLISHING INTEGRATION INSIGHTS---');
$this->publisher->publish('ddg.sync.integration', '');
$this->publisher->publish(IntegrationInsights::TOPIC_SYNC_INTEGRATION, '');

return $this->resultFactory->create(ResultFactory::TYPE_RAW)
->setHttpResponseCode(201);
Expand Down
4 changes: 3 additions & 1 deletion Model/Queue/Sync/Automation/AutomationPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class AutomationPublisher
{
public const TOPIC_SYNC_AUTOMATION = 'ddg.sync.automation';

/**
* @var AutomationDataFactory
*/
Expand Down Expand Up @@ -45,6 +47,6 @@ public function publish(Automation $automation)
$message->setId((int) $automation->getId());
$message->setType($automation->getAutomationType());

$this->publisher->publish('ddg.sync.automation', $message);
$this->publisher->publish(self::TOPIC_SYNC_AUTOMATION, $message);
}
}
2 changes: 2 additions & 0 deletions Model/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Subscriber
{
public const TOPIC_NEWSLETTER_SUBSCRIPTION = 'ddg.newsletter.subscription';

/**
* @var Contact
*/
Expand Down
2 changes: 2 additions & 0 deletions Model/Sync/Integration/IntegrationInsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class IntegrationInsights implements SyncInterface
{
public const TOPIC_SYNC_INTEGRATION = 'ddg.sync.integration';

/**
* @var IntegrationInsightData
*/
Expand Down
3 changes: 2 additions & 1 deletion Observer/Adminhtml/AccountCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Dotdigitalgroup\Email\Helper\Data;
use Dotdigitalgroup\Email\Model\Apiconnector\Test;
use Dotdigitalgroup\Email\Model\Sync\Integration\IntegrationInsights;
use Magento\Backend\App\Action\Context;
use Magento\Config\Model\ResourceModel\Config;
use Magento\Framework\Event\Observer;
Expand Down Expand Up @@ -109,7 +110,7 @@ public function execute(Observer $observer)
$isValidAccount = $this->isValidAccount($apiUsername, $apiPassword);
if ($isValidAccount) {
$this->helper->log('----PUBLISHING INTEGRATION INSIGHTS---');
$this->publisher->publish('ddg.sync.integration', '');
$this->publisher->publish(IntegrationInsights::TOPIC_SYNC_INTEGRATION, '');

$websiteId = $this->context->getRequest()->getParam('website');

Expand Down
4 changes: 3 additions & 1 deletion Observer/Customer/CreateUpdateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class CreateUpdateContact implements \Magento\Framework\Event\ObserverInterface
{
private const TOPIC_CONTACT_EMAIL_UPDATE = 'ddg.contact.email_update';

/**
* @var \Dotdigitalgroup\Email\Model\ResourceModel\Contact
*/
Expand Down Expand Up @@ -241,7 +243,7 @@ private function checkForEmailUpdate(Contact $contactModel, string $newEmail)
$emailUpdateData->setEmail($newEmail);
$emailUpdateData->setWebsiteId((int) $contactModel->getWebsiteId());

$this->publisher->publish('ddg.contact.email_update', $emailUpdateData);
$this->publisher->publish(self::TOPIC_CONTACT_EMAIL_UPDATE, $emailUpdateData);
}

return $contactModel;
Expand Down
3 changes: 2 additions & 1 deletion Observer/Customer/RemoveContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Dotdigitalgroup\Email\Model\Queue\Data\SubscriptionDataFactory;
use Dotdigitalgroup\Email\Model\Importer;
use Dotdigitalgroup\Email\Model\ResourceModel\Contact;
use Dotdigitalgroup\Email\Model\Subscriber as DotdigitalSubscriber;
use Magento\Framework\MessageQueue\PublisherInterface;
use Magento\Store\Model\StoreManagerInterface;

Expand Down Expand Up @@ -137,7 +138,7 @@ private function unsubscribeCustomer(string $email, int $websiteId)
$unsubscriber->setWebsiteId($websiteId);
$unsubscriber->setType('unsubscribe');

$this->publisher->publish('ddg.newsletter.subscription', $unsubscriber);
$this->publisher->publish(DotdigitalSubscriber::TOPIC_NEWSLETTER_SUBSCRIPTION, $unsubscriber);
} catch (\Exception $e) {
$this->helper->debug('Error when unsubscribing a customer', [(string) $e]);
}
Expand Down
7 changes: 4 additions & 3 deletions Observer/Newsletter/ChangeContactSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Dotdigitalgroup\Email\Model\ResourceModel\Automation\CollectionFactory;
use Dotdigitalgroup\Email\Model\ResourceModel\Contact;
use Dotdigitalgroup\Email\Model\StatusInterface;
use Dotdigitalgroup\Email\Model\Subscriber as DotdigitalSubscriber;
use Dotdigitalgroup\Email\Model\Sync\Automation\AutomationTypeHandler;
use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
Expand Down Expand Up @@ -211,7 +212,7 @@ public function execute(Observer $observer)
$resubscribeData->setEmail($email);
$resubscribeData->setWebsiteId($websiteId);
$resubscribeData->setType('resubscribe');
$this->publisher->publish('ddg.newsletter.subscription', $resubscribeData);
$this->publisher->publish(DotdigitalSubscriber::TOPIC_NEWSLETTER_SUBSCRIPTION, $resubscribeData);
} else {
// save first in order to have a row id for the queue publish
$this->contactResource->save($contactEmail);
Expand All @@ -221,7 +222,7 @@ public function execute(Observer $observer)
$subscribeData->setEmail($email);
$subscribeData->setWebsiteId($websiteId);
$subscribeData->setType('subscribe');
$this->publisher->publish('ddg.newsletter.subscription', $subscribeData);
$this->publisher->publish(DotdigitalSubscriber::TOPIC_NEWSLETTER_SUBSCRIPTION, $subscribeData);
}
//not subscribed
} else {
Expand All @@ -243,7 +244,7 @@ public function execute(Observer $observer)
$unsubscriber->setWebsiteId($websiteId);
$unsubscriber->setType('unsubscribe');

$this->publisher->publish('ddg.newsletter.subscription', $unsubscriber);
$this->publisher->publish(DotdigitalSubscriber::TOPIC_NEWSLETTER_SUBSCRIPTION, $unsubscriber);
}

$this->saveContactAsNotSubscribed($contactEmail);
Expand Down
4 changes: 3 additions & 1 deletion Observer/Sales/OrderPlaceAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
class OrderPlaceAfter implements ObserverInterface
{
private const TOPIC_SALES_CART_PHASE_UPDATE = 'ddg.sales.cart_phase_update';

/**
* @var Data
*/
Expand Down Expand Up @@ -223,6 +225,6 @@ private function queueCartPhaseUpdate(OrderInterface $order)
$cartPhaseUpdateData->setQuoteId((int) $order->getQuoteId());
$cartPhaseUpdateData->setStoreId($order->getStoreId());

$this->publisher->publish('ddg.sales.cart_phase_update', $cartPhaseUpdateData);
$this->publisher->publish(self::TOPIC_SALES_CART_PHASE_UPDATE, $cartPhaseUpdateData);
}
}
3 changes: 2 additions & 1 deletion Observer/Sales/OrderSaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Dotdigitalgroup\Email\Model\ResourceModel\Automation;
use Dotdigitalgroup\Email\Model\ResourceModel\Automation\CollectionFactory;
use Dotdigitalgroup\Email\Model\StatusInterface;
use Dotdigitalgroup\Email\Model\Subscriber as DotdigitalSubscriber;
use Dotdigitalgroup\Email\Model\Sync\Automation\AutomationTypeHandler;
use Exception;
use InvalidArgumentException;
Expand Down Expand Up @@ -282,7 +283,7 @@ private function resetContact($contact)
$subscriptionData->setEmail($contact->getEmail());
$subscriptionData->setWebsiteId($contact->getWebsiteId());
$subscriptionData->setType('subscribe');
$this->publisher->publish('ddg.newsletter.subscription', $subscriptionData);
$this->publisher->publish(DotdigitalSubscriber::TOPIC_NEWSLETTER_SUBSCRIPTION, $subscriptionData);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testPublish(): void

$this->publisherMock->expects($this->once())
->method('publish')
->with('ddg.sync.automation', $messageMock);
->with(AutomationPublisher::TOPIC_SYNC_AUTOMATION, $messageMock);

$this->automationPublisher->publish($automationMock);
}
Expand Down

0 comments on commit 10ea75a

Please sign in to comment.