ReCaptchaObserver.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Observer;
  21. use Magento\Framework\Event\Observer;
  22. use Magento\Framework\Event\ObserverInterface;
  23. use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
  24. use MSP\ReCaptcha\Api\ValidateInterface;
  25. use MSP\ReCaptcha\Model\IsCheckRequiredInterface;
  26. use MSP\ReCaptcha\Model\Provider\FailureProviderInterface;
  27. use MSP\ReCaptcha\Model\Provider\ResponseProviderInterface;
  28. class ReCaptchaObserver implements ObserverInterface
  29. {
  30. /**
  31. * @var FailureProviderInterface
  32. */
  33. private $failureProvider;
  34. /**
  35. * @var ValidateInterface
  36. */
  37. private $validate;
  38. /**
  39. * @var RemoteAddress
  40. */
  41. private $remoteAddress;
  42. /**
  43. * @var ResponseProviderInterface
  44. */
  45. private $responseProvider;
  46. /**
  47. * @var IsCheckRequiredInterface
  48. */
  49. private $isCheckRequired;
  50. /**
  51. * ReCaptchaObserver constructor.
  52. * @param ResponseProviderInterface $responseProvider
  53. * @param ValidateInterface $validate
  54. * @param FailureProviderInterface $failureProvider
  55. * @param RemoteAddress $remoteAddress
  56. * @param IsCheckRequiredInterface $isCheckRequired
  57. */
  58. public function __construct(
  59. ResponseProviderInterface $responseProvider,
  60. ValidateInterface $validate,
  61. FailureProviderInterface $failureProvider,
  62. RemoteAddress $remoteAddress,
  63. IsCheckRequiredInterface $isCheckRequired
  64. ) {
  65. $this->responseProvider = $responseProvider;
  66. $this->validate = $validate;
  67. $this->failureProvider = $failureProvider;
  68. $this->remoteAddress = $remoteAddress;
  69. $this->isCheckRequired = $isCheckRequired;
  70. }
  71. /**
  72. * @param Observer $observer
  73. * @return void
  74. */
  75. public function execute(Observer $observer)
  76. {
  77. if ($this->isCheckRequired->execute()) {
  78. $reCaptchaResponse = $this->responseProvider->execute();
  79. $remoteIp = $this->remoteAddress->getRemoteAddress();
  80. /** @var \Magento\Framework\App\Action\Action $controller */
  81. $controller = $observer->getControllerAction();
  82. if (!$this->validate->validate($reCaptchaResponse, $remoteIp)) {
  83. $this->failureProvider->execute($controller ? $controller->getResponse(): null);
  84. }
  85. }
  86. }
  87. }