10
10
use Magento \Framework \App \ObjectManager ;
11
11
use Magento \Framework \Exception \NoSuchEntityException ;
12
12
use Magento \Framework \Controller \ResultFactory ;
13
- use Magento \Framework \Exception \LocalizedException ;
14
- use Magento \Captcha \Helper \Data ;
15
- use Magento \Captcha \Model \DefaultModel ;
16
- use Magento \Captcha \Observer \CaptchaStringResolver ;
17
- use Magento \Authorization \Model \UserContextInterface ;
18
- use Magento \Customer \Api \CustomerRepositoryInterface ;
13
+ use Magento \SendFriend \Model \CaptchaValidator ;
19
14
20
15
/**
21
16
* Class Sendmail. Represents request flow logic of 'sendmail' feature
@@ -35,24 +30,9 @@ class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPos
35
30
protected $ catalogSession ;
36
31
37
32
/**
38
- * @var Data
33
+ * @var CaptchaValidator
39
34
*/
40
- private $ captchaHelper ;
41
-
42
- /**
43
- * @var CaptchaStringResolver
44
- */
45
- private $ captchaStringResolver ;
46
-
47
- /**
48
- * @var UserContextInterface
49
- */
50
- private $ currentUser ;
51
-
52
- /**
53
- * @var CustomerRepositoryInterface
54
- */
55
- private $ customerRepository ;
35
+ private $ captchaValidator ;
56
36
57
37
/**
58
38
* Sendmail class construct
@@ -64,12 +44,7 @@ class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPos
64
44
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
65
45
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
66
46
* @param \Magento\Catalog\Model\Session $catalogSession
67
- * @param Data|null $captchaHelper
68
- * @param CaptchaStringResolver|null $captchaStringResolver
69
- * @param UserContextInterface|null $currentUser
70
- * @param CustomerRepositoryInterface|null $customerRepository
71
- *
72
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
47
+ * @param CaptchaValidator|null $captchaValidator
73
48
*/
74
49
public function __construct (
75
50
\Magento \Framework \App \Action \Context $ context ,
@@ -79,20 +54,12 @@ public function __construct(
79
54
\Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
80
55
\Magento \Catalog \Api \CategoryRepositoryInterface $ categoryRepository ,
81
56
\Magento \Catalog \Model \Session $ catalogSession ,
82
- ?Data $ captchaHelper = null ,
83
- ?CaptchaStringResolver $ captchaStringResolver = null ,
84
- ?UserContextInterface $ currentUser = null ,
85
- ?CustomerRepositoryInterface $ customerRepository = null
57
+ CaptchaValidator $ captchaValidator = null
86
58
) {
87
59
parent ::__construct ($ context , $ coreRegistry , $ formKeyValidator , $ sendFriend , $ productRepository );
88
60
$ this ->categoryRepository = $ categoryRepository ;
89
61
$ this ->catalogSession = $ catalogSession ;
90
- $ this ->captchaHelper = $ captchaHelper ?: ObjectManager::getInstance ()->create (Data::class);
91
- $ this ->captchaStringResolver = $ captchaStringResolver ?:
92
- ObjectManager::getInstance ()->create (CaptchaStringResolver::class);
93
- $ this ->currentUser = $ currentUser ?: ObjectManager::getInstance ()->get (UserContextInterface::class);
94
- $ this ->customerRepository = $ customerRepository ?:
95
- ObjectManager::getInstance ()->create (CustomerRepositoryInterface::class);
62
+ $ this ->captchaValidator = $ captchaValidator ?: ObjectManager::getInstance ()->create (CaptchaValidator::class);
96
63
}
97
64
98
65
/**
@@ -137,7 +104,7 @@ public function execute()
137
104
try {
138
105
$ validate = $ this ->sendFriend ->validate ();
139
106
140
- $ this ->validateCaptcha ( );
107
+ $ this ->captchaValidator -> validateSending ( $ this -> getRequest () );
141
108
142
109
if ($ validate === true ) {
143
110
$ this ->sendFriend ->send ();
@@ -167,48 +134,48 @@ public function execute()
167
134
$ resultRedirect ->setUrl ($ this ->_redirect ->error ($ url ));
168
135
return $ resultRedirect ;
169
136
}
170
-
171
- /**
172
- * Method validates captcha, if it's enabled for target form
173
- *
174
- * @throws LocalizedException
175
- */
176
- private function validateCaptcha () : void
177
- {
178
- $ captchaTargetFormName = 'product_sendtofriend_form ' ;
179
- /** @var DefaultModel $captchaModel */
180
- $ captchaModel = $ this ->captchaHelper ->getCaptcha ($ captchaTargetFormName );
181
-
182
- if ($ captchaModel ->isRequired ()) {
183
- $ word = $ this ->captchaStringResolver ->resolve (
184
- $ this ->getRequest (),
185
- $ captchaTargetFormName
186
- );
187
-
188
- $ isCorrectCaptcha = $ captchaModel ->isCorrect ($ word );
189
-
190
- if (!$ isCorrectCaptcha ) {
191
- $ this ->logCaptchaAttempt ($ captchaModel );
192
- throw new LocalizedException (__ ('Incorrect CAPTCHA ' ));
193
- }
194
- }
195
-
196
- $ this ->logCaptchaAttempt ($ captchaModel );
197
- }
198
-
199
- /**
200
- * Log captcha attempts
201
- *
202
- * @param DefaultModel $captchaModel
203
- */
204
- private function logCaptchaAttempt (DefaultModel $ captchaModel ) : void
205
- {
206
- $ email = '' ;
207
-
208
- if ($ this ->currentUser ->getUserType () == UserContextInterface::USER_TYPE_CUSTOMER ) {
209
- $ email = $ this ->customerRepository ->getById ($ this ->currentUser ->getUserId ())->getEmail ();
210
- }
211
-
212
- $ captchaModel ->logAttempt ($ email );
213
- }
137
+ //
138
+ // /**
139
+ // * Method validates captcha, if it's enabled for target form
140
+ // *
141
+ // * @throws LocalizedException
142
+ // */
143
+ // private function validateCaptcha() : void
144
+ // {
145
+ // $captchaTargetFormName = 'product_sendtofriend_form';
146
+ // /** @var DefaultModel $captchaModel */
147
+ // $captchaModel = $this->captchaHelper->getCaptcha($captchaTargetFormName);
148
+ //
149
+ // if ($captchaModel->isRequired()) {
150
+ // $word = $this->captchaStringResolver->resolve(
151
+ // $this->getRequest(),
152
+ // $captchaTargetFormName
153
+ // );
154
+ //
155
+ // $isCorrectCaptcha = $captchaModel->isCorrect($word);
156
+ //
157
+ // if (!$isCorrectCaptcha) {
158
+ // $this->logCaptchaAttempt($captchaModel);
159
+ // throw new LocalizedException(__('Incorrect CAPTCHA'));
160
+ // }
161
+ // }
162
+ //
163
+ // $this->logCaptchaAttempt($captchaModel);
164
+ // }
165
+ //
166
+ // /**
167
+ // * Log captcha attempts
168
+ // *
169
+ // * @param DefaultModel $captchaModel
170
+ // */
171
+ // private function logCaptchaAttempt(DefaultModel $captchaModel) : void
172
+ // {
173
+ // $email = '';
174
+ //
175
+ // if ($this->currentUser->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
176
+ // $email = $this->customerRepository->getById($this->currentUser->getUserId())->getEmail();
177
+ // }
178
+ //
179
+ // $captchaModel->logAttempt($email);
180
+ // }
214
181
}
0 commit comments