|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Refreshes captcha and returns JSON encoded URL to image (AJAX action) |
4 | | - * Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'} |
5 | | - * |
6 | 3 | * Copyright © Magento, Inc. All rights reserved. |
7 | 4 | * See COPYING.txt for license details. |
8 | 5 | */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
9 | 8 | namespace Magento\Captcha\Controller\Refresh; |
10 | 9 |
|
11 | | -use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; |
12 | | -use Magento\Framework\App\Action\Context; |
| 10 | +use Magento\Captcha\Helper\Data as CaptchaHelper; |
| 11 | +use Magento\Framework\App\Action\HttpPostActionInterface; |
| 12 | +use Magento\Framework\App\RequestInterface; |
| 13 | +use Magento\Framework\Controller\Result\JsonFactory as JsonResultFactory; |
| 14 | +use Magento\Framework\Serialize\Serializer\Json as JsonSerializer; |
| 15 | +use Magento\Framework\View\LayoutInterface; |
13 | 16 |
|
14 | | -class Index extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface |
| 17 | +/** |
| 18 | + * Refreshes captcha and returns JSON encoded URL to image (AJAX action) |
| 19 | + * Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'} |
| 20 | + */ |
| 21 | +class Index implements HttpPostActionInterface |
15 | 22 | { |
16 | 23 | /** |
17 | | - * @var \Magento\Captcha\Helper\Data |
| 24 | + * @var CaptchaHelper |
| 25 | + */ |
| 26 | + private $captchaHelper; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var JsonSerializer |
| 30 | + */ |
| 31 | + private $serializer; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var RequestInterface |
| 35 | + */ |
| 36 | + private $request; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var LayoutInterface |
18 | 40 | */ |
19 | | - protected $captchaHelper; |
| 41 | + private $layout; |
20 | 42 |
|
21 | 43 | /** |
22 | | - * @var \Magento\Framework\Serialize\Serializer\Json |
| 44 | + * @var JsonResultFactory |
23 | 45 | */ |
24 | | - protected $serializer; |
| 46 | + private $jsonResultFactory; |
25 | 47 |
|
26 | 48 | /** |
27 | | - * @param Context $context |
28 | | - * @param \Magento\Captcha\Helper\Data $captchaHelper |
29 | | - * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer |
30 | | - * @throws \RuntimeException |
| 49 | + * @param RequestInterface $request |
| 50 | + * @param JsonResultFactory $jsonFactory |
| 51 | + * @param CaptchaHelper $captchaHelper |
| 52 | + * @param LayoutInterface $layout |
| 53 | + * @param JsonSerializer $serializer |
31 | 54 | */ |
32 | 55 | public function __construct( |
33 | | - Context $context, |
34 | | - \Magento\Captcha\Helper\Data $captchaHelper, |
35 | | - \Magento\Framework\Serialize\Serializer\Json $serializer = null |
| 56 | + RequestInterface $request, |
| 57 | + JsonResultFactory $jsonFactory, |
| 58 | + CaptchaHelper $captchaHelper, |
| 59 | + LayoutInterface $layout, |
| 60 | + JsonSerializer $serializer |
36 | 61 | ) { |
| 62 | + $this->request = $request; |
| 63 | + $this->jsonResultFactory = $jsonFactory; |
37 | 64 | $this->captchaHelper = $captchaHelper; |
38 | | - $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() |
39 | | - ->get(\Magento\Framework\Serialize\Serializer\Json::class); |
40 | | - parent::__construct($context); |
| 65 | + $this->layout = $layout; |
| 66 | + $this->serializer = $serializer; |
41 | 67 | } |
42 | 68 |
|
43 | 69 | /** |
44 | | - * {@inheritdoc} |
| 70 | + * @inheritdoc |
45 | 71 | */ |
46 | 72 | public function execute() |
47 | 73 | { |
48 | | - $formId = $this->_request->getPost('formId'); |
| 74 | + $formId = $this->getRequestFormId(); |
| 75 | + |
| 76 | + $captchaModel = $this->captchaHelper->getCaptcha($formId); |
| 77 | + $captchaModel->generate(); |
| 78 | + |
| 79 | + $block = $this->layout->createBlock($captchaModel->getBlockName()); |
| 80 | + $block->setFormId($formId)->setIsAjax(true)->toHtml(); |
| 81 | + |
| 82 | + $result = $this->jsonResultFactory->create(); |
| 83 | + |
| 84 | + return $result->setData(['imgSrc' => $captchaModel->getImgSrc()]); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns requested Form ID |
| 89 | + * |
| 90 | + * @return string|null |
| 91 | + */ |
| 92 | + private function getRequestFormId(): ?string |
| 93 | + { |
| 94 | + $formId = $this->request->getPost('formId'); |
49 | 95 | if (null === $formId) { |
50 | 96 | $params = []; |
51 | | - $content = $this->_request->getContent(); |
| 97 | + $content = $this->request->getContent(); |
52 | 98 | if ($content) { |
53 | 99 | $params = $this->serializer->unserialize($content); |
54 | 100 | } |
55 | | - $formId = isset($params['formId']) ? $params['formId'] : null; |
| 101 | + |
| 102 | + $formId = $params['formId'] ?? null; |
56 | 103 | } |
57 | | - $captchaModel = $this->captchaHelper->getCaptcha($formId); |
58 | | - $captchaModel->generate(); |
59 | 104 |
|
60 | | - $block = $this->_view->getLayout()->createBlock($captchaModel->getBlockName()); |
61 | | - $block->setFormId($formId)->setIsAjax(true)->toHtml(); |
62 | | - $this->_response->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()])); |
63 | | - $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true); |
| 105 | + return $formId !== null ? (string)$formId : null; |
64 | 106 | } |
65 | 107 | } |
0 commit comments