Skip to content

Commit

Permalink
added method for removing a subscriber from the queue into the resour…
Browse files Browse the repository at this point in the history
…ce model; refactor
  • Loading branch information
engcom-Charlie committed Feb 17, 2021
1 parent 35468fc commit 23bfcdd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

namespace Magento\Newsletter\Model\Plugin;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Newsletter\Model\ResourceModel\Queue as QueueResource;
use Magento\Newsletter\Model\Subscriber;

/**
Expand All @@ -19,16 +18,16 @@ class RemoveSubscriberFromQueue
private const STATUS = 'subscriber_status';

/**
* @var AdapterInterface
* @var QueueResource
*/
private $connection;
private $queueResource;

/**
* @param ResourceConnection $resource
* @param QueueResource $queueResource
*/
public function __construct(ResourceConnection $resource)
public function __construct(QueueResource $queueResource)
{
$this->connection = $resource->getConnection();
$this->queueResource = $queueResource;
}

/**
Expand All @@ -41,13 +40,8 @@ public function __construct(ResourceConnection $resource)
*/
public function afterUnsubscribe(Subscriber $subject, Subscriber $subscriber): Subscriber
{
if ($subscriber->dataHasChangedFor(self::STATUS)
&& $subscriber->getSubscriberStatus() === Subscriber::STATUS_UNSUBSCRIBED
) {
$this->connection->delete(
$this->connection->getTableName('newsletter_queue_link'),
['subscriber_id = ?' => $subscriber->getId(), 'letter_sent_at IS NULL']
);
if ($subscriber->isStatusChanged() && $subscriber->getSubscriberStatus() === Subscriber::STATUS_UNSUBSCRIBED) {
$this->queueResource->removeSubscriberFromQueue((int) $subscriber->getId());
}

return $subscriber;
Expand Down
16 changes: 15 additions & 1 deletion app/code/Magento/Newsletter/Model/ResourceModel/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function addSubscribersToQueue(ModelQueue $queue, array $subscriberIds)
$usedIds = array_flip($connection->fetchCol($select));
$subscriberIds = array_flip($subscriberIds);
$newIds = array_diff_key($subscriberIds, $usedIds);

$connection->beginTransaction();
try {
foreach (array_keys($newIds) as $subscriberId) {
Expand Down Expand Up @@ -125,6 +125,20 @@ public function removeSubscribersFromQueue(ModelQueue $queue)
}
}

/**
* Removes subscriber from queue
*
* @param int $subscriberId
* @return void
*/
public function removeSubscriberFromQueue(int $subscriberId): void
{
$this->getConnection()->delete(
$this->getTable('newsletter_queue_link'),
['subscriber_id = ?' => $subscriberId, 'letter_sent_at IS NULL']
);
}

/**
* Links queue to store
*
Expand Down
7 changes: 6 additions & 1 deletion app/code/Magento/Newsletter/Model/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ public function getStatus()
*/
public function setStatus($value)
{
if ($this->getSubscriberStatus() !== $value) {
$this->setStatusChanged(true);
}

return $this->setSubscriberStatus($value);
}

Expand Down Expand Up @@ -449,7 +453,8 @@ public function unsubscribe()
}

if ($this->getSubscriberStatus() != self::STATUS_UNSUBSCRIBED) {
$this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)->save();
$this->setStatus(self::STATUS_UNSUBSCRIBED);
$this->save();
$this->sendUnsubscriptionEmail();
}
return $this;
Expand Down
5 changes: 1 addition & 4 deletions app/code/Magento/Newsletter/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
type="Magento\Newsletter\Model\Plugin\CustomerPlugin"/>
</type>
<type name="Magento\Newsletter\Model\Subscriber">
<plugin name="remove_subscriber_from_queue_after_unsubscribe" type="Magento\Newsletter\Model\Plugin\RemoveSubscriberFromQueue"/>
<arguments>
<argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session\Proxy</argument>
</arguments>
</type>
<preference for="Magento\Newsletter\Model\SubscriptionManagerInterface"
type="Magento\Newsletter\Model\SubscriptionManager"/>
<type name="Magento\Newsletter\Model\Subscriber">
<plugin name="remove_subscriber_from_queue_after_unsubscribe"
type="Magento\Newsletter\Model\Plugin\RemoveSubscriberFromQueue"/>
</type>
</config>

0 comments on commit 23bfcdd

Please sign in to comment.