3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \SalesRule \Controller \Adminhtml \Promo \Quote ;
7
9
10
+ use Magento \Backend \App \Action \Context ;
8
11
use Magento \Framework \App \Action \HttpPostActionInterface ;
12
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
13
+ use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \App \Response \Http \FileFactory ;
15
+ use Magento \Framework \Exception \InputException ;
16
+ use Magento \Framework \Exception \LocalizedException ;
9
17
use Magento \Framework \Filter \FilterInput ;
18
+ use Magento \Framework \Json \Helper \Data ;
10
19
use Magento \Framework \MessageQueue \PublisherInterface ;
20
+ use Magento \Framework \Registry ;
21
+ use Magento \Framework \Stdlib \DateTime \Filter \Date ;
11
22
use Magento \SalesRule \Api \Data \CouponGenerationSpecInterfaceFactory ;
23
+ use Magento \SalesRule \Controller \Adminhtml \Promo \Quote ;
12
24
use Magento \SalesRule \Model \CouponGenerator ;
13
25
use Magento \SalesRule \Model \Quote \GetCouponCodeLengthInterface ;
26
+ use Magento \SalesRule \Model \RegistryConstants ;
27
+ use Magento \SalesRule \Model \Rule ;
28
+ use Magento \Store \Model \ScopeInterface ;
29
+ use Psr \Log \LoggerInterface ;
14
30
15
31
/**
16
32
* Generate promo quote
17
33
*
18
34
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
35
*/
20
- class Generate extends \ Magento \ SalesRule \ Controller \ Adminhtml \ Promo \ Quote implements HttpPostActionInterface
36
+ class Generate extends Quote implements HttpPostActionInterface
21
37
{
38
+ /**
39
+ * Coupon quantity limit config path
40
+ */
41
+ private const XML_CONFIG_COUPON_QUANTITY_LIMIT = 'promo/auto_generated_coupon_codes/quantity_limit ' ;
42
+
22
43
/**
23
44
* @var CouponGenerator
24
45
*/
25
- private $ couponGenerator ;
46
+ private CouponGenerator $ couponGenerator ;
26
47
27
48
/**
28
49
* @var PublisherInterface
29
50
*/
30
- private $ messagePublisher ;
51
+ private PublisherInterface $ messagePublisher ;
31
52
32
53
/**
33
54
* @var CouponGenerationSpecInterfaceFactory
34
55
*/
35
- private $ generationSpecFactory ;
56
+ private CouponGenerationSpecInterfaceFactory $ generationSpecFactory ;
36
57
37
58
/**
38
59
* @var GetCouponCodeLengthInterface
39
60
*/
40
- private $ getCouponCodeLength ;
61
+ private GetCouponCodeLengthInterface $ getCouponCodeLength ;
62
+
63
+ /**
64
+ * @var ScopeConfigInterface
65
+ */
66
+ private ScopeConfigInterface $ scopeConfig ;
41
67
42
68
/**
43
69
* Generate constructor.
44
- * @param \Magento\Backend\App\Action\ Context $context
45
- * @param \Magento\Framework\ Registry $coreRegistry
46
- * @param \Magento\Framework\App\Response\Http\ FileFactory $fileFactory
47
- * @param \Magento\Framework\Stdlib\DateTime\Filter\ Date $dateFilter
70
+ * @param Context $context
71
+ * @param Registry $coreRegistry
72
+ * @param FileFactory $fileFactory
73
+ * @param Date $dateFilter
48
74
* @param CouponGenerator|null $couponGenerator
49
75
* @param PublisherInterface|null $publisher
50
76
* @param CouponGenerationSpecInterfaceFactory|null $generationSpecFactory
51
77
* @param GetCouponCodeLengthInterface|null $getCouponCodeLength
78
+ * @param ScopeConfigInterface|null $scopeConfig
52
79
*/
53
80
public function __construct (
54
- \ Magento \ Backend \ App \ Action \ Context $ context ,
55
- \ Magento \ Framework \ Registry $ coreRegistry ,
56
- \ Magento \ Framework \ App \ Response \ Http \ FileFactory $ fileFactory ,
57
- \ Magento \ Framework \ Stdlib \ DateTime \ Filter \ Date $ dateFilter ,
81
+ Context $ context ,
82
+ Registry $ coreRegistry ,
83
+ FileFactory $ fileFactory ,
84
+ Date $ dateFilter ,
58
85
CouponGenerator $ couponGenerator = null ,
59
86
PublisherInterface $ publisher = null ,
60
87
CouponGenerationSpecInterfaceFactory $ generationSpecFactory = null ,
61
- GetCouponCodeLengthInterface $ getCouponCodeLength = null
88
+ GetCouponCodeLengthInterface $ getCouponCodeLength = null ,
89
+ ScopeConfigInterface $ scopeConfig = null
62
90
) {
63
91
parent ::__construct ($ context , $ coreRegistry , $ fileFactory , $ dateFilter );
64
92
$ this ->couponGenerator = $ couponGenerator ?:
65
93
$ this ->_objectManager ->get (CouponGenerator::class);
66
- $ this ->messagePublisher = $ publisher ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
94
+ $ this ->messagePublisher = $ publisher ?: ObjectManager::getInstance ()
67
95
->get (PublisherInterface::class);
68
96
$ this ->generationSpecFactory = $ generationSpecFactory ?:
69
- \ Magento \ Framework \ App \ ObjectManager::getInstance ()->get (
97
+ ObjectManager::getInstance ()->get (
70
98
CouponGenerationSpecInterfaceFactory::class
71
99
);
72
100
$ this ->getCouponCodeLength = $ getCouponCodeLength ?:
73
- \ Magento \ Framework \ App \ ObjectManager::getInstance ()->get (
101
+ ObjectManager::getInstance ()->get (
74
102
GetCouponCodeLengthInterface::class
75
103
);
104
+ $ this ->scopeConfig = $ scopeConfig ;
76
105
}
77
106
78
107
/**
@@ -81,7 +110,7 @@ public function __construct(
81
110
* @return void
82
111
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
83
112
*/
84
- public function execute ()
113
+ public function execute (): void
85
114
{
86
115
if (!$ this ->getRequest ()->isAjax ()) {
87
116
$ this ->_forward ('noroute ' );
@@ -90,13 +119,13 @@ public function execute()
90
119
$ result = [];
91
120
$ this ->_initRule ();
92
121
93
- $ rule = $ this ->_coreRegistry ->registry (\ Magento \ SalesRule \ Model \ RegistryConstants::CURRENT_SALES_RULE );
122
+ $ rule = $ this ->_coreRegistry ->registry (RegistryConstants::CURRENT_SALES_RULE );
94
123
95
124
$ data = $ this ->getRequest ()->getParams ();
96
125
97
126
if (!$ rule ->getId ()) {
98
127
$ result ['error ' ] = __ ('Rule is not defined ' );
99
- } elseif ((int ) $ rule ->getCouponType () !== \ Magento \ SalesRule \ Model \ Rule::COUPON_TYPE_AUTO
128
+ } elseif ((int ) $ rule ->getCouponType () !== Rule::COUPON_TYPE_AUTO
100
129
&& !$ rule ->getUseAutoGeneration ()) {
101
130
$ result ['error ' ] =
102
131
__ ('The rule coupon settings changed. Please save the rule before using auto-generation. ' );
@@ -123,7 +152,7 @@ public function execute()
123
152
$ result ['error ' ] = __ (
124
153
'Something went wrong while validating coupon code length. Please review the log and try again. '
125
154
);
126
- $ this ->_objectManager ->get (\ Psr \ Log \ LoggerInterface::class)->critical ($ e );
155
+ $ this ->_objectManager ->get (LoggerInterface::class)->critical ($ e );
127
156
}
128
157
} else {
129
158
try {
@@ -132,29 +161,42 @@ public function execute()
132
161
$ data = $ inputFilter ->getUnescaped ();
133
162
}
134
163
135
- $ data ['quantity ' ] = isset ($ data ['qty ' ]) ? $ data ['qty ' ] : null ;
136
-
137
- $ couponSpec = $ this ->generationSpecFactory ->create (['data ' => $ data ]);
138
-
139
- $ this ->messagePublisher ->publish ('sales_rule.codegenerator ' , $ couponSpec );
140
- $ this ->messageManager ->addSuccessMessage (
141
- __ ('Message is added to queue, wait to get your coupons soon ' )
164
+ $ data ['quantity ' ] = $ data ['qty ' ] ?? 0 ;
165
+ $ couponQuantityLimit = (int )$ this ->scopeConfig ->getValue (
166
+ self ::XML_CONFIG_COUPON_QUANTITY_LIMIT ,
167
+ ScopeInterface::SCOPE_STORE
142
168
);
169
+ // @codingStandardsIgnoreStart
170
+ if ($ data ['quantity ' ] > 0 && ($ data ['quantity ' ] <= $ couponQuantityLimit || $ couponQuantityLimit === 0 )) {
171
+ $ couponSpec = $ this ->generationSpecFactory ->create (['data ' => $ data ]);
172
+
173
+ $ this ->messagePublisher ->publish ('sales_rule.codegenerator ' , $ couponSpec );
174
+ $ this ->messageManager ->addSuccessMessage (
175
+ __ ('Message is added to queue, wait to get your coupons soon. ' )
176
+ );
177
+ } else {
178
+ $ this ->messageManager ->addErrorMessage (
179
+ __ (
180
+ 'Coupon quantity should be less than or equal to the coupon quantity in the store configuration. '
181
+ )
182
+ );
183
+ }
184
+ // @codingStandardsIgnoreEnd
143
185
$ this ->_view ->getLayout ()->initMessages ();
144
186
$ result ['messages ' ] = $ this ->_view ->getLayout ()->getMessagesBlock ()->getGroupedHtml ();
145
- } catch (\ Magento \ Framework \ Exception \ InputException $ inputException ) {
187
+ } catch (InputException $ inputException ) {
146
188
$ result ['error ' ] = __ ('Invalid data provided ' );
147
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
189
+ } catch (LocalizedException $ e ) {
148
190
$ result ['error ' ] = $ e ->getMessage ();
149
191
} catch (\Exception $ e ) {
150
192
$ result ['error ' ] = __ (
151
193
'Something went wrong while generating coupons. Please review the log and try again. '
152
194
);
153
- $ this ->_objectManager ->get (\ Psr \ Log \ LoggerInterface::class)->critical ($ e );
195
+ $ this ->_objectManager ->get (LoggerInterface::class)->critical ($ e );
154
196
}
155
197
}
156
198
$ this ->getResponse ()->representJson (
157
- $ this ->_objectManager ->get (\ Magento \ Framework \ Json \ Helper \ Data::class)->jsonEncode ($ result )
199
+ $ this ->_objectManager ->get (Data::class)->jsonEncode ($ result )
158
200
);
159
201
}
160
202
}
0 commit comments