Skip to content

Commit 23bfcdd

Browse files
added method for removing a subscriber from the queue into the resource model; refactor
1 parent 35468fc commit 23bfcdd

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

app/code/Magento/Newsletter/Model/Plugin/RemoveSubscriberFromQueue.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
namespace Magento\Newsletter\Model\Plugin;
99

10-
use Magento\Framework\App\ResourceConnection;
11-
use Magento\Framework\DB\Adapter\AdapterInterface;
10+
use Magento\Newsletter\Model\ResourceModel\Queue as QueueResource;
1211
use Magento\Newsletter\Model\Subscriber;
1312

1413
/**
@@ -19,16 +18,16 @@ class RemoveSubscriberFromQueue
1918
private const STATUS = 'subscriber_status';
2019

2120
/**
22-
* @var AdapterInterface
21+
* @var QueueResource
2322
*/
24-
private $connection;
23+
private $queueResource;
2524

2625
/**
27-
* @param ResourceConnection $resource
26+
* @param QueueResource $queueResource
2827
*/
29-
public function __construct(ResourceConnection $resource)
28+
public function __construct(QueueResource $queueResource)
3029
{
31-
$this->connection = $resource->getConnection();
30+
$this->queueResource = $queueResource;
3231
}
3332

3433
/**
@@ -41,13 +40,8 @@ public function __construct(ResourceConnection $resource)
4140
*/
4241
public function afterUnsubscribe(Subscriber $subject, Subscriber $subscriber): Subscriber
4342
{
44-
if ($subscriber->dataHasChangedFor(self::STATUS)
45-
&& $subscriber->getSubscriberStatus() === Subscriber::STATUS_UNSUBSCRIBED
46-
) {
47-
$this->connection->delete(
48-
$this->connection->getTableName('newsletter_queue_link'),
49-
['subscriber_id = ?' => $subscriber->getId(), 'letter_sent_at IS NULL']
50-
);
43+
if ($subscriber->isStatusChanged() && $subscriber->getSubscriberStatus() === Subscriber::STATUS_UNSUBSCRIBED) {
44+
$this->queueResource->removeSubscriberFromQueue((int) $subscriber->getId());
5145
}
5246

5347
return $subscriber;

app/code/Magento/Newsletter/Model/ResourceModel/Queue.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function addSubscribersToQueue(ModelQueue $queue, array $subscriberIds)
8686
$usedIds = array_flip($connection->fetchCol($select));
8787
$subscriberIds = array_flip($subscriberIds);
8888
$newIds = array_diff_key($subscriberIds, $usedIds);
89-
89+
9090
$connection->beginTransaction();
9191
try {
9292
foreach (array_keys($newIds) as $subscriberId) {
@@ -125,6 +125,20 @@ public function removeSubscribersFromQueue(ModelQueue $queue)
125125
}
126126
}
127127

128+
/**
129+
* Removes subscriber from queue
130+
*
131+
* @param int $subscriberId
132+
* @return void
133+
*/
134+
public function removeSubscriberFromQueue(int $subscriberId): void
135+
{
136+
$this->getConnection()->delete(
137+
$this->getTable('newsletter_queue_link'),
138+
['subscriber_id = ?' => $subscriberId, 'letter_sent_at IS NULL']
139+
);
140+
}
141+
128142
/**
129143
* Links queue to store
130144
*

app/code/Magento/Newsletter/Model/Subscriber.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ public function getStatus()
308308
*/
309309
public function setStatus($value)
310310
{
311+
if ($this->getSubscriberStatus() !== $value) {
312+
$this->setStatusChanged(true);
313+
}
314+
311315
return $this->setSubscriberStatus($value);
312316
}
313317

@@ -449,7 +453,8 @@ public function unsubscribe()
449453
}
450454

451455
if ($this->getSubscriberStatus() != self::STATUS_UNSUBSCRIBED) {
452-
$this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)->save();
456+
$this->setStatus(self::STATUS_UNSUBSCRIBED);
457+
$this->save();
453458
$this->sendUnsubscriptionEmail();
454459
}
455460
return $this;

app/code/Magento/Newsletter/etc/di.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626
type="Magento\Newsletter\Model\Plugin\CustomerPlugin"/>
2727
</type>
2828
<type name="Magento\Newsletter\Model\Subscriber">
29+
<plugin name="remove_subscriber_from_queue_after_unsubscribe" type="Magento\Newsletter\Model\Plugin\RemoveSubscriberFromQueue"/>
2930
<arguments>
3031
<argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session\Proxy</argument>
3132
</arguments>
3233
</type>
3334
<preference for="Magento\Newsletter\Model\SubscriptionManagerInterface"
3435
type="Magento\Newsletter\Model\SubscriptionManager"/>
35-
<type name="Magento\Newsletter\Model\Subscriber">
36-
<plugin name="remove_subscriber_from_queue_after_unsubscribe"
37-
type="Magento\Newsletter\Model\Plugin\RemoveSubscriberFromQueue"/>
38-
</type>
3936
</config>

0 commit comments

Comments
 (0)