IsCheckRequired.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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;
  21. use Magento\Framework\App\Area;
  22. use Magento\Framework\App\Config\ScopeConfigInterface;
  23. use Magento\Framework\App\RequestInterface;
  24. class IsCheckRequired implements IsCheckRequiredInterface
  25. {
  26. /**
  27. * @var ScopeConfigInterface
  28. */
  29. private $scopeConfig;
  30. /**
  31. * @var Config
  32. */
  33. private $config;
  34. /**
  35. * @var string
  36. */
  37. private $enableConfigFlag;
  38. /**
  39. * @var bool
  40. */
  41. private $requireRequestParam;
  42. /**
  43. * @var string
  44. */
  45. private $area;
  46. /**
  47. * @var RequestInterface
  48. */
  49. private $request;
  50. /**
  51. * IsCheckRequired constructor.
  52. * @param ScopeConfigInterface $scopeConfig
  53. * @param RequestInterface $request
  54. * @param Config $config
  55. * @param string $area
  56. * @param string $enableConfigFlag
  57. * @param bool $requireRequestParam
  58. */
  59. public function __construct(
  60. ScopeConfigInterface $scopeConfig,
  61. RequestInterface $request,
  62. Config $config,
  63. $area = null,
  64. $enableConfigFlag = null,
  65. $requireRequestParam = null
  66. ) {
  67. $this->scopeConfig = $scopeConfig;
  68. $this->config = $config;
  69. $this->enableConfigFlag = $enableConfigFlag;
  70. $this->requireRequestParam = $requireRequestParam;
  71. $this->area = $area;
  72. $this->request = $request;
  73. if (!in_array($this->area, [Area::AREA_FRONTEND, Area::AREA_ADMINHTML])) {
  74. throw new \InvalidArgumentException('Area parameter must be one of frontend or adminhtml');
  75. }
  76. }
  77. /**
  78. * Return true if area is configured to be active
  79. * @return bool
  80. */
  81. private function isAreaEnabled()
  82. {
  83. return
  84. (($this->area === Area::AREA_ADMINHTML) && $this->config->isEnabledBackend()) ||
  85. (($this->area === Area::AREA_FRONTEND) && $this->config->isEnabledFrontend());
  86. }
  87. /**
  88. * Return true if current zone is enabled
  89. * @return bool
  90. */
  91. private function isZoneEnabled()
  92. {
  93. return !$this->enableConfigFlag || $this->scopeConfig->getValue($this->enableConfigFlag);
  94. }
  95. /**
  96. * Return true if request if valid
  97. * @return bool
  98. */
  99. private function isRequestValid()
  100. {
  101. return !$this->requireRequestParam || $this->request->getParam($this->requireRequestParam);
  102. }
  103. /**
  104. * Return true if check is required
  105. * @return bool
  106. */
  107. public function execute()
  108. {
  109. return
  110. $this->isAreaEnabled() &&
  111. $this->isZoneEnabled() &&
  112. $this->isRequestValid();
  113. }
  114. }