88namespace Magento \ReCaptchaCustomer \Observer ;
99
1010use Magento \Framework \App \Action \Action ;
11- use Magento \Framework \App \ActionFlag ;
12- use Magento \Framework \App \ResponseInterface ;
1311use Magento \Framework \Event \Observer ;
1412use Magento \Framework \Event \ObserverInterface ;
1513use Magento \Framework \Exception \InputException ;
1614use Magento \Framework \Exception \LocalizedException ;
17- use Magento \Framework \ Serialize \ SerializerInterface ;
15+ use Magento \ReCaptchaCustomer \ Model \ AjaxLogin \ ErrorProcessor ;
1816use Magento \ReCaptchaUi \Model \CaptchaResponseResolverInterface ;
1917use Magento \ReCaptchaUi \Model \IsCaptchaEnabledInterface ;
2018use Magento \ReCaptchaUi \Model \ValidationConfigResolverInterface ;
@@ -41,16 +39,6 @@ class AjaxLoginObserver implements ObserverInterface
4139 */
4240 private $ captchaValidator ;
4341
44- /**
45- * @var ActionFlag
46- */
47- private $ actionFlag ;
48-
49- /**
50- * @var SerializerInterface
51- */
52- private $ serializer ;
53-
5442 /**
5543 * @var IsCaptchaEnabledInterface
5644 */
@@ -61,31 +49,33 @@ class AjaxLoginObserver implements ObserverInterface
6149 */
6250 private $ logger ;
6351
52+ /**
53+ * @var ErrorProcessor
54+ */
55+ private $ errorProcessor ;
56+
6457 /**
6558 * @param CaptchaResponseResolverInterface $captchaResponseResolver
6659 * @param ValidationConfigResolverInterface $validationConfigResolver
6760 * @param ValidatorInterface $captchaValidator
68- * @param ActionFlag $actionFlag
69- * @param SerializerInterface $serializer
7061 * @param IsCaptchaEnabledInterface $isCaptchaEnabled
7162 * @param LoggerInterface $logger
63+ * @param ErrorProcessor $errorProcessor
7264 */
7365 public function __construct (
7466 CaptchaResponseResolverInterface $ captchaResponseResolver ,
7567 ValidationConfigResolverInterface $ validationConfigResolver ,
7668 ValidatorInterface $ captchaValidator ,
77- ActionFlag $ actionFlag ,
78- SerializerInterface $ serializer ,
7969 IsCaptchaEnabledInterface $ isCaptchaEnabled ,
80- LoggerInterface $ logger
70+ LoggerInterface $ logger ,
71+ ErrorProcessor $ errorProcessor
8172 ) {
8273 $ this ->captchaResponseResolver = $ captchaResponseResolver ;
8374 $ this ->validationConfigResolver = $ validationConfigResolver ;
8475 $ this ->captchaValidator = $ captchaValidator ;
85- $ this ->actionFlag = $ actionFlag ;
86- $ this ->serializer = $ serializer ;
8776 $ this ->isCaptchaEnabled = $ isCaptchaEnabled ;
8877 $ this ->logger = $ logger ;
78+ $ this ->errorProcessor = $ errorProcessor ;
8979 }
9080
9181 /**
@@ -101,36 +91,27 @@ public function execute(Observer $observer): void
10191 $ controller = $ observer ->getControllerAction ();
10292 $ request = $ controller ->getRequest ();
10393 $ response = $ controller ->getResponse ();
94+
10495 $ validationConfig = $ this ->validationConfigResolver ->get ($ key );
10596
10697 try {
10798 $ reCaptchaResponse = $ this ->captchaResponseResolver ->resolve ($ request );
10899 } catch (InputException $ e ) {
109100 $ this ->logger ->error ($ e );
110- $ this ->processError ($ response , $ validationConfig ->getValidationFailureMessage ());
101+ $ this ->errorProcessor ->processError (
102+ $ response ,
103+ $ validationConfig ->getValidationFailureMessage ()
104+ );
111105 return ;
112106 }
113107
114108 $ validationResult = $ this ->captchaValidator ->isValid ($ reCaptchaResponse , $ validationConfig );
115109 if (false === $ validationResult ->isValid ()) {
116- $ this ->processError ($ response , $ validationConfig ->getValidationFailureMessage ());
110+ $ this ->errorProcessor ->processError (
111+ $ response ,
112+ $ validationConfig ->getValidationFailureMessage ()
113+ );
117114 }
118115 }
119116 }
120-
121- /**
122- * @param ResponseInterface $response
123- * @param string $message
124- * @return void
125- */
126- private function processError (ResponseInterface $ response , string $ message ): void
127- {
128- $ this ->actionFlag ->set ('' , Action::FLAG_NO_DISPATCH , true );
129-
130- $ jsonPayload = $ this ->serializer ->serialize ([
131- 'errors ' => true ,
132- 'message ' => $ message ,
133- ]);
134- $ response ->representJson ($ jsonPayload );
135- }
136117}
0 commit comments