AjaxResponseProvider.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_ReCaptcha
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\ReCaptcha\Model\Provider\Response;
  21. use Magento\Framework\App\RequestInterface;
  22. use Magento\Framework\Json\DecoderInterface;
  23. use MSP\ReCaptcha\Model\Provider\ResponseProviderInterface;
  24. class AjaxResponseProvider implements ResponseProviderInterface
  25. {
  26. /**
  27. * @var RequestInterface
  28. */
  29. private $request;
  30. /**
  31. * @var DecoderInterface
  32. */
  33. private $decoder;
  34. /**
  35. * AjaxResponseProvider constructor.
  36. * @param RequestInterface $request
  37. * @param DecoderInterface $decoder
  38. */
  39. public function __construct(
  40. RequestInterface $request,
  41. DecoderInterface $decoder
  42. ) {
  43. $this->request = $request;
  44. $this->decoder = $decoder;
  45. }
  46. /**
  47. * Handle reCaptcha failure
  48. * @return string
  49. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  50. */
  51. public function execute()
  52. {
  53. if ($content = $this->request->getContent()) {
  54. try {
  55. $jsonParams = $this->decoder->decode($content);
  56. if (isset($jsonParams['g-recaptcha-response'])) {
  57. return $jsonParams['g-recaptcha-response'];
  58. }
  59. } catch (\Exception $e) {
  60. return '';
  61. }
  62. }
  63. }
  64. }