Skip to content

Commit 8529979

Browse files
committed
Merge branch 'ACP2E-2734-V2' of https://github.com/adobe-commerce-tier-4/magento2ce into 2.4-develop
2 parents e040e1a + bc03a5d commit 8529979

File tree

4 files changed

+87
-47
lines changed

4 files changed

+87
-47
lines changed

app/code/Magento/Sales/Model/EmailSenderHandler.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
*/
66
namespace Magento\Sales\Model;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
89
use Magento\Framework\App\Config\ValueFactory;
910
use Magento\Framework\App\Config\ValueInterface;
1011
use Magento\Framework\App\ObjectManager;
1112
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
13+
use Magento\Sales\Model\Order\Email\Sender;
1214
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
15+
use Magento\Sales\Model\ResourceModel\EntityAbstract;
16+
use Magento\Store\Model\StoreManagerInterface;
1317

1418
/**
1519
* Sales emails sending
@@ -19,17 +23,22 @@
1923
*/
2024
class EmailSenderHandler
2125
{
26+
/**
27+
* Configuration path for defining asynchronous email sending attempts
28+
*/
29+
public const XML_PATH_ASYNC_SENDING_ATTEMPTS = 'sales_email/general/async_sending_attempts';
30+
2231
/**
2332
* Email sender model.
2433
*
25-
* @var \Magento\Sales\Model\Order\Email\Sender
34+
* @var Sender
2635
*/
2736
protected $emailSender;
2837

2938
/**
3039
* Entity resource model.
3140
*
32-
* @var \Magento\Sales\Model\ResourceModel\EntityAbstract
41+
* @var EntityAbstract
3342
*/
3443
protected $entityResource;
3544

@@ -43,7 +52,7 @@ class EmailSenderHandler
4352
/**
4453
* Global configuration storage.
4554
*
46-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
55+
* @var ScopeConfigInterface
4756
*/
4857
protected $globalConfig;
4958

@@ -53,7 +62,7 @@ class EmailSenderHandler
5362
private $identityContainer;
5463

5564
/**
56-
* @var \Magento\Store\Model\StoreManagerInterface
65+
* @var StoreManagerInterface
5766
*/
5867
private $storeManager;
5968

@@ -70,24 +79,24 @@ class EmailSenderHandler
7079
private $modifyStartFromDate;
7180

7281
/**
73-
* @param \Magento\Sales\Model\Order\Email\Sender $emailSender
74-
* @param \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource
82+
* @param Sender $emailSender
83+
* @param EntityAbstract $entityResource
7584
* @param AbstractCollection $entityCollection
76-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig
85+
* @param ScopeConfigInterface $globalConfig
7786
* @param IdentityInterface|null $identityContainer
78-
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
87+
* @param StoreManagerInterface|null $storeManager
7988
* @param ValueFactory|null $configValueFactory
8089
* @param string|null $modifyStartFromDate
8190
*/
8291
public function __construct(
83-
\Magento\Sales\Model\Order\Email\Sender $emailSender,
84-
\Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource,
92+
Sender $emailSender,
93+
EntityAbstract $entityResource,
8594
AbstractCollection $entityCollection,
86-
\Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
95+
ScopeConfigInterface $globalConfig,
8796
IdentityInterface $identityContainer = null,
88-
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
97+
StoreManagerInterface $storeManager = null,
8998
?ValueFactory $configValueFactory = null,
90-
?string $modifyStartFromDate = null
99+
?string $modifyStartFromDate = null,
91100
) {
92101
$this->emailSender = $emailSender;
93102
$this->entityResource = $entityResource;
@@ -97,21 +106,28 @@ public function __construct(
97106
$this->identityContainer = $identityContainer ?: ObjectManager::getInstance()
98107
->get(\Magento\Sales\Model\Order\Email\Container\NullIdentity::class);
99108
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
100-
->get(\Magento\Store\Model\StoreManagerInterface::class);
109+
->get(StoreManagerInterface::class);
101110

102111
$this->configValueFactory = $configValueFactory ?: ObjectManager::getInstance()->get(ValueFactory::class);
103112
$this->modifyStartFromDate = $modifyStartFromDate ?: $this->modifyStartFromDate;
104113
}
105114

106115
/**
107116
* Handles asynchronous email sending
117+
*
108118
* @return void
109119
*/
110120
public function sendEmails()
111121
{
112122
if ($this->globalConfig->getValue('sales_email/general/async_sending')) {
113123
$this->entityCollection->addFieldToFilter('send_email', ['eq' => 1]);
114-
$this->entityCollection->addFieldToFilter('email_sent', ['null' => true]);
124+
$this->entityCollection->addFieldToFilter(
125+
'email_sent',
126+
[
127+
['null' => true],
128+
['lteq' => -1]
129+
]
130+
);
115131
$this->filterCollectionByStartFromDate($this->entityCollection);
116132
$this->entityCollection->setPageSize(
117133
$this->globalConfig->getValue('sales_email/general/sending_limit')
@@ -120,6 +136,8 @@ public function sendEmails()
120136
/** @var \Magento\Store\Api\Data\StoreInterface[] $stores */
121137
$stores = $this->getStores(clone $this->entityCollection);
122138

139+
$maxSendAttempts = $this->globalConfig->getValue(self::XML_PATH_ASYNC_SENDING_ATTEMPTS);
140+
123141
/** @var \Magento\Store\Model\Store $store */
124142
foreach ($stores as $store) {
125143
$this->identityContainer->setStore($store);
@@ -131,12 +149,19 @@ public function sendEmails()
131149

132150
/** @var \Magento\Sales\Model\AbstractModel $item */
133151
foreach ($entityCollection->getItems() as $item) {
134-
if ($this->emailSender->send($item, true)) {
135-
$this->entityResource->saveAttribute(
136-
$item->setEmailSent(true),
137-
'email_sent'
138-
);
152+
$sendAttempts = $item->getEmailSent() ?? -$maxSendAttempts;
153+
$isEmailSent = $this->emailSender->send($item, true);
154+
155+
if ($isEmailSent) {
156+
$sendAttempts = 1;
157+
} else {
158+
$sendAttempts++;
139159
}
160+
161+
$this->entityResource->saveAttribute(
162+
$item->setEmailSent($sendAttempts),
163+
'email_sent'
164+
);
140165
}
141166
}
142167
}
@@ -157,7 +182,7 @@ private function getStores(
157182
$entityCollection->addAttributeToSelect('store_id')->getSelect()->group('store_id');
158183
/** @var \Magento\Sales\Model\EntityInterface $item */
159184
foreach ($entityCollection->getItems() as $item) {
160-
/** @var \Magento\Store\Model\StoreManagerInterface $store */
185+
/** @var StoreManagerInterface $store */
161186
$store = $this->storeManager->getStore($item->getStoreId());
162187
$stores[$item->getStoreId()] = $store;
163188
}

app/code/Magento/Sales/Test/Unit/Model/EmailSenderHandlerTest.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ protected function setUp(): void
151151
* @param int $configValue
152152
* @param array|null $collectionItems
153153
* @param bool|null $emailSendingResult
154+
* @param int|null $expectedIsEmailSent
154155
*
155156
* @return void
156157
* @dataProvider executeDataProvider
@@ -159,14 +160,20 @@ protected function setUp(): void
159160
public function testExecute(
160161
int $configValue,
161162
?array $collectionItems,
162-
?bool $emailSendingResult
163+
?bool $emailSendingResult,
164+
?int $expectedIsEmailSent
163165
): void {
164-
$path = 'sales_email/general/async_sending';
165-
166166
$this->globalConfig
167167
->method('getValue')
168-
->withConsecutive([$path])
169-
->willReturnOnConsecutiveCalls($configValue);
168+
->willReturnCallback(function ($path) use ($configValue) {
169+
if ($path === 'sales_email/general/async_sending') {
170+
return $configValue;
171+
}
172+
if ($path === 'sales_email/general/async_sending_attempts') {
173+
return 3;
174+
}
175+
return null;
176+
});
170177

171178
if ($configValue) {
172179
$nowDate = date('Y-m-d H:i:s');
@@ -175,7 +182,12 @@ public function testExecute(
175182
->method('addFieldToFilter')
176183
->withConsecutive(
177184
['send_email', ['eq' => 1]],
178-
['email_sent', ['null' => true]],
185+
['email_sent',
186+
[
187+
['null' => true],
188+
['lteq' => -1]
189+
]
190+
],
179191
['created_at', ['from' => $fromDate]]
180192
);
181193

@@ -245,18 +257,16 @@ public function testExecute(
245257
->method('isEnabled')
246258
->willReturn(true);
247259

248-
if ($emailSendingResult) {
249-
$collectionItem
250-
->expects($this->once())
251-
->method('setEmailSent')
252-
->with(true)
253-
->willReturn($collectionItem);
254-
255-
$this->entityResource
256-
->expects($this->once())
257-
->method('saveAttribute')
258-
->with($collectionItem);
259-
}
260+
$collectionItem
261+
->expects($this->once())
262+
->method('setEmailSent')
263+
->with($expectedIsEmailSent)
264+
->willReturn($collectionItem);
265+
266+
$this->entityResource
267+
->expects($this->once())
268+
->method('saveAttribute')
269+
->with($collectionItem);
260270
}
261271
}
262272

@@ -282,22 +292,26 @@ public function executeDataProvider(): array
282292
[
283293
'configValue' => 1,
284294
'collectionItems' => [clone $entityModel],
285-
'emailSendingResult' => true
295+
'emailSendingResult' => true,
296+
'expectedIsEmailSent' => 1
286297
],
287298
[
288299
'configValue' => 1,
289300
'collectionItems' => [clone $entityModel],
290-
'emailSendingResult' => false
301+
'emailSendingResult' => false,
302+
'expectedIsEmailSent' => -2
291303
],
292304
[
293305
'configValue' => 1,
294306
'collectionItems' => [],
295-
'emailSendingResult' => null
307+
'emailSendingResult' => null,
308+
'expectedIsEmailSent' => 1
296309
],
297310
[
298311
'configValue' => 0,
299312
'collectionItems' => null,
300-
'emailSendingResult' => null
313+
'emailSendingResult' => null,
314+
'expectedIsEmailSent' => 1
301315
]
302316
];
303317
}

app/code/Magento/Sales/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<sales_email>
3737
<general>
3838
<async_sending>0</async_sending>
39+
<async_sending_attempts>3</async_sending_attempts>
3940
<sending_limit>50</sending_limit>
4041
</general>
4142
<order>

app/code/Magento/Sales/etc/db_schema.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<column xsi:type="int" name="customer_group_id" unsigned="false" nullable="true" identity="false"/>
150150
<column xsi:type="int" name="edit_increment" unsigned="false" nullable="true" identity="false"
151151
comment="Edit Increment"/>
152-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
152+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
153153
comment="Email Sent"/>
154154
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
155155
comment="Send Email"/>
@@ -708,7 +708,7 @@
708708
comment="Total Weight"/>
709709
<column xsi:type="decimal" name="total_qty" scale="4" precision="12" unsigned="false" nullable="true"
710710
comment="Total Qty"/>
711-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
711+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
712712
comment="Email Sent"/>
713713
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
714714
comment="Send Email"/>
@@ -981,7 +981,7 @@
981981
identity="false" comment="Is Used For Refund"/>
982982
<column xsi:type="int" name="order_id" unsigned="true" nullable="false" identity="false"
983983
comment="Order ID"/>
984-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
984+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
985985
comment="Email Sent"/>
986986
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
987987
comment="Send Email"/>
@@ -1278,7 +1278,7 @@
12781278
comment="Tax Amount"/>
12791279
<column xsi:type="int" name="order_id" unsigned="true" nullable="false" identity="false"
12801280
comment="Order ID"/>
1281-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
1281+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
12821282
comment="Email Sent"/>
12831283
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
12841284
comment="Send Email"/>

0 commit comments

Comments
 (0)