Skip to content

Commit c3ec2e0

Browse files
committed
Merge remote-tracking branch 'performance/MC-13615' into MC-5953-2
2 parents c48826f + 04e68fb commit c3ec2e0

File tree

10 files changed

+209
-10
lines changed

10 files changed

+209
-10
lines changed

app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/Generate.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
use Magento\Framework\App\Action\HttpPostActionInterface;
99
use Magento\SalesRule\Model\CouponGenerator;
10+
use Magento\Framework\MessageQueue\PublisherInterface;
11+
use Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory;
1012

1113
/**
1214
* Generate promo quote
15+
*
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1317
*/
1418
class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote implements HttpPostActionInterface
1519
{
@@ -18,24 +22,44 @@ class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote imple
1822
*/
1923
private $couponGenerator;
2024

25+
/**
26+
* @var PublisherInterface
27+
*/
28+
private $messagePublisher;
29+
30+
/**
31+
* @var CouponGenerationSpecInterfaceFactory
32+
*/
33+
private $generationSpecFactory;
34+
2135
/**
2236
* Generate constructor.
2337
* @param \Magento\Backend\App\Action\Context $context
2438
* @param \Magento\Framework\Registry $coreRegistry
2539
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
2640
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
2741
* @param CouponGenerator|null $couponGenerator
42+
* @param PublisherInterface|null $publisher
43+
* @param CouponGenerationSpecInterfaceFactory|null $generationSpecFactory
2844
*/
2945
public function __construct(
3046
\Magento\Backend\App\Action\Context $context,
3147
\Magento\Framework\Registry $coreRegistry,
3248
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
3349
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
34-
CouponGenerator $couponGenerator = null
50+
CouponGenerator $couponGenerator = null,
51+
PublisherInterface $publisher = null,
52+
CouponGenerationSpecInterfaceFactory $generationSpecFactory = null
3553
) {
3654
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
3755
$this->couponGenerator = $couponGenerator ?:
3856
$this->_objectManager->get(CouponGenerator::class);
57+
$this->messagePublisher = $publisher ?: \Magento\Framework\App\ObjectManager::getInstance()
58+
->get(PublisherInterface::class);
59+
$this->generationSpecFactory = $generationSpecFactory ?:
60+
\Magento\Framework\App\ObjectManager::getInstance()->get(
61+
CouponGenerationSpecInterfaceFactory::class
62+
);
3963
}
4064

4165
/**
@@ -64,9 +88,14 @@ public function execute()
6488
$data = $inputFilter->getUnescaped();
6589
}
6690

67-
$couponCodes = $this->couponGenerator->generateCodes($data);
68-
$generated = count($couponCodes);
69-
$this->messageManager->addSuccessMessage(__('%1 coupon(s) have been generated.', $generated));
91+
$data['quantity'] = isset($data['qty']) ? $data['qty'] : null;
92+
93+
$couponSpec = $this->generationSpecFactory->create(['data' => $data]);
94+
95+
$this->messagePublisher->publish('sales_rule.codegenerator', $couponSpec);
96+
$this->messageManager->addSuccessMessage(
97+
__('Message is added to queue, wait to get your coupons soon')
98+
);
7099
$this->_view->getLayout()->initMessages();
71100
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
72101
} catch (\Magento\Framework\Exception\InputException $inputException) {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Model\Coupon;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Filesystem;
12+
use Magento\SalesRule\Api\CouponManagementInterface;
13+
use Magento\SalesRule\Api\Data\CouponGenerationSpecInterface;
14+
use Magento\Framework\Notification\NotifierInterface;
15+
16+
/**
17+
* Consumer for export coupons generation.
18+
*/
19+
class Consumer
20+
{
21+
/**
22+
* @var NotifierInterface
23+
*/
24+
private $notifier;
25+
26+
/**
27+
* @var \Psr\Log\LoggerInterface
28+
*/
29+
private $logger;
30+
31+
/**
32+
* @var CouponManagementInterface
33+
*/
34+
private $couponManager;
35+
36+
/**
37+
* @var Filesystem
38+
*/
39+
private $filesystem;
40+
41+
/**
42+
* Consumer constructor.
43+
* @param \Psr\Log\LoggerInterface $logger
44+
* @param CouponManagementInterface $couponManager
45+
* @param Filesystem $filesystem
46+
* @param NotifierInterface $notifier
47+
*/
48+
public function __construct(
49+
\Psr\Log\LoggerInterface $logger,
50+
CouponManagementInterface $couponManager,
51+
Filesystem $filesystem,
52+
NotifierInterface $notifier
53+
) {
54+
$this->logger = $logger;
55+
$this->couponManager = $couponManager;
56+
$this->filesystem = $filesystem;
57+
$this->notifier = $notifier;
58+
}
59+
60+
/**
61+
* Consumer logic.
62+
*
63+
* @param CouponGenerationSpecInterface $exportInfo
64+
* @return void
65+
*/
66+
public function process(CouponGenerationSpecInterface $exportInfo)
67+
{
68+
try {
69+
$this->couponManager->generate($exportInfo);
70+
71+
$this->notifier->addMajor(
72+
__('Your coupons are ready'),
73+
__('You can check your coupons at sales rule page')
74+
);
75+
} catch (LocalizedException $exception) {
76+
$this->notifier->addCritical(
77+
__('Error during coupons generator process occurred'),
78+
__('Error during coupons generator process occurred. Please check logs for detail')
79+
);
80+
$this->logger->critical(
81+
'Something went wrong while coupons generator process. ' . $exception->getMessage()
82+
);
83+
}
84+
}
85+
}

app/code/Magento/SalesRule/Test/Mftf/Test/AdminCreateCartPriceRuleForGeneratedCouponTest.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@
5353
<click selector="{{AdminCartPriceRulesFormSection.manageCouponCodesHeader}}" stepKey="expandCouponSection"/>
5454
<fillField selector="{{AdminCartPriceRulesFormSection.couponQty}}" userInput="10" stepKey="fillCouponQty"/>
5555
<click selector="{{AdminCartPriceRulesFormSection.generateCouponsButton}}" stepKey="clickGenerate"/>
56-
<see selector="{{AdminCartPriceRulesFormSection.successMessage}}" userInput="10 coupon(s) have been generated." stepKey="seeGenerationSuccess"/>
56+
<see selector="{{AdminCartPriceRulesFormSection.successMessage}}" userInput="Message is added to queue, wait to get your coupons soon" stepKey="seeGenerationSuccess"/>
57+
58+
<!-- Run cron twice -->
59+
<magentoCLI command="cron:run" stepKey="runCron1"/>
60+
<magentoCLI command="cron:run" stepKey="runCron2"/>
61+
<reloadPage stepKey="refreshPage"/>
62+
<waitForPageLoad stepKey="waitFormToReload1"/>
63+
<click selector="{{AdminCartPriceRulesFormSection.manageCouponCodesHeader}}" stepKey="expandCouponSection2"/>
5764

5865
<!-- Grab a coupon code and hold on to it for later -->
5966
<grabTextFrom selector="{{AdminCartPriceRulesFormSection.generatedCouponByIndex('1')}}" stepKey="grabCouponCode"/>

app/code/Magento/SalesRule/Test/Mftf/Test/StorefrontAutoGeneratedCouponCodeTest.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,19 @@
5656
stepKey="clickManageCouponCodes"/>
5757
<fillField selector="{{AdminCartPriceRulesFormSection.couponQty}}" userInput="1" stepKey="fillFieldCouponQty"/>
5858
<click selector="{{AdminCartPriceRulesFormSection.generateCouponsButton}}" stepKey="clickGenerateCoupon"/>
59-
<see selector="{{AdminCartPriceRulesFormSection.successMessage}}" userInput="1 coupon(s) have been generated."
59+
<see selector="{{AdminCartPriceRulesFormSection.successMessage}}" userInput="Message is added to queue, wait to get your coupons soon"
6060
stepKey="seeSuccessMessage"/>
61+
62+
<!-- Run cron twice -->
63+
<magentoCLI command="cron:run" stepKey="runCron1"/>
64+
<magentoCLI command="cron:run" stepKey="runCron2"/>
65+
<reloadPage stepKey="refreshPage"/>
66+
<waitForPageLoad stepKey="waitFormToReload1"/>
67+
<conditionalClick selector="{{AdminCartPriceRulesFormSection.manageCouponCodesHeader}}"
68+
dependentSelector="{{AdminCartPriceRulesFormSection.manageCouponCodesHeader}}" visible="true"
69+
stepKey="clickManageCouponCodes2"/>
70+
71+
<!-- Grab a coupon code and hold on to it for later -->
6172
<grabTextFrom selector="{{AdminCartPriceRulesFormSection.generatedCouponByIndex('1')}}"
6273
stepKey="couponCode"/>
6374

app/code/Magento/SalesRule/Test/Unit/Controller/Adminhtml/Promo/Quote/GenerateTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\App\ObjectManager;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1010
use Magento\SalesRule\Model\CouponGenerator;
11+
use Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory;
1112

1213
/**
1314
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -50,6 +51,9 @@ class GenerateTest extends \PHPUnit\Framework\TestCase
5051
/** @var CouponGenerator | \PHPUnit_Framework_MockObject_MockObject */
5152
private $couponGenerator;
5253

54+
/** @var CouponGenerationSpecInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject */
55+
private $couponGenerationSpec;
56+
5357
/**
5458
* Test setup
5559
*/
@@ -98,6 +102,9 @@ protected function setUp()
98102
$this->couponGenerator = $this->getMockBuilder(CouponGenerator::class)
99103
->disableOriginalConstructor()
100104
->getMock();
105+
$this->couponGenerationSpec = $this->getMockBuilder(CouponGenerationSpecInterfaceFactory::class)
106+
->disableOriginalConstructor()
107+
->getMock();
101108

102109
$this->objectManagerHelper = new ObjectManagerHelper($this);
103110
$this->model = $this->objectManagerHelper->getObject(
@@ -107,7 +114,8 @@ protected function setUp()
107114
'coreRegistry' => $this->registryMock,
108115
'fileFactory' => $this->fileFactoryMock,
109116
'dateFilter' => $this->dateMock,
110-
'couponGenerator' => $this->couponGenerator
117+
'couponGenerator' => $this->couponGenerator,
118+
'generationSpecFactory' => $this->couponGenerationSpec
111119
]
112120
);
113121
}
@@ -144,9 +152,10 @@ public function testExecute()
144152
$this->requestMock->expects($this->once())
145153
->method('getParams')
146154
->willReturn($requestData);
147-
$this->couponGenerator->expects($this->once())
148-
->method('generateCodes')
149-
->with($requestData)
155+
$requestData['quantity'] = isset($requestData['qty']) ? $requestData['qty'] : null;
156+
$this->couponGenerationSpec->expects($this->once())
157+
->method('create')
158+
->with(['data' => $requestData])
150159
->willReturn(['some_data', 'some_data_2']);
151160
$this->messageManager->expects($this->once())
152161
->method('addSuccessMessage');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Communication/etc/communication.xsd">
9+
<topic name="sales_rule.codegenerator" request="Magento\SalesRule\Api\Data\CouponGenerationSpecInterface">
10+
<handler name="codegeneratorProcessor" type="Magento\SalesRule\Model\Coupon\Consumer" method="process" />
11+
</topic>
12+
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/queue.xsd">
9+
<broker topic="sales_rule.codegenerator" exchange="magento-db" type="db">
10+
<queue name="codegenerator" consumer="codegeneratorProcessor" consumerInstance="Magento\Framework\MessageQueue\Consumer" handler="Magento\SalesRule\Model\Coupon\Consumer::process"/>
11+
</broker>
12+
</config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/consumer.xsd">
9+
<consumer name="codegeneratorProcessor" queue="codegenerator" connection="db" maxMessages="5000" consumerInstance="Magento\Framework\MessageQueue\Consumer" handler="Magento\SalesRule\Model\Coupon\Consumer::process" />
10+
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
9+
<publisher topic="sales_rule.codegenerator">
10+
<connection name="db" exchange="magento-db" />
11+
</publisher>
12+
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/topology.xsd">
9+
<exchange name="magento-db" type="topic" connection="db">
10+
<binding id="codegeneratorBinding" topic="sales_rule.codegenerator" destinationType="queue" destination="codegenerator"/>
11+
</exchange>
12+
</config>

0 commit comments

Comments
 (0)