ConfigProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Model\Checkout;
  7. /**
  8. * Configuration provider for Captcha rendering.
  9. */
  10. class ConfigProvider implements \Magento\Checkout\Model\ConfigProviderInterface
  11. {
  12. /**
  13. * @var \Magento\Store\Model\StoreManagerInterface
  14. */
  15. protected $storeManager;
  16. /**
  17. * @var \Magento\Captcha\Helper\Data
  18. */
  19. protected $captchaData;
  20. /**
  21. * @var array
  22. */
  23. protected $formIds;
  24. /**
  25. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  26. * @param \Magento\Captcha\Helper\Data $captchaData
  27. * @param array $formIds
  28. */
  29. public function __construct(
  30. \Magento\Store\Model\StoreManagerInterface $storeManager,
  31. \Magento\Captcha\Helper\Data $captchaData,
  32. array $formIds
  33. ) {
  34. $this->storeManager = $storeManager;
  35. $this->captchaData = $captchaData;
  36. $this->formIds = $formIds;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function getConfig()
  42. {
  43. $config = [];
  44. foreach ($this->formIds as $formId) {
  45. $config['captcha'][$formId] = [
  46. 'isCaseSensitive' => $this->isCaseSensitive($formId),
  47. 'imageHeight' => $this->getImageHeight($formId),
  48. 'imageSrc' => $this->getImageSrc($formId),
  49. 'refreshUrl' => $this->getRefreshUrl(),
  50. 'isRequired' => $this->isRequired($formId),
  51. 'timestamp' => time()
  52. ];
  53. }
  54. return $config;
  55. }
  56. /**
  57. * Returns is captcha case sensitive
  58. *
  59. * @param string $formId
  60. * @return bool
  61. */
  62. protected function isCaseSensitive($formId)
  63. {
  64. return (boolean)$this->getCaptchaModel($formId)->isCaseSensitive();
  65. }
  66. /**
  67. * Returns captcha image height
  68. *
  69. * @param string $formId
  70. * @return int
  71. */
  72. protected function getImageHeight($formId)
  73. {
  74. return $this->getCaptchaModel($formId)->getHeight();
  75. }
  76. /**
  77. * Returns captcha image source path
  78. *
  79. * @param string $formId
  80. * @return string
  81. */
  82. protected function getImageSrc($formId)
  83. {
  84. if ($this->isRequired($formId)) {
  85. $captcha = $this->getCaptchaModel($formId);
  86. $captcha->generate();
  87. return $captcha->getImgSrc();
  88. }
  89. return '';
  90. }
  91. /**
  92. * Returns URL to controller action which returns new captcha image
  93. *
  94. * @return string
  95. */
  96. protected function getRefreshUrl()
  97. {
  98. $store = $this->storeManager->getStore();
  99. return $store->getUrl('captcha/refresh', ['_secure' => $store->isCurrentlySecure()]);
  100. }
  101. /**
  102. * Whether captcha is required to be inserted to this form
  103. *
  104. * @param string $formId
  105. * @return bool
  106. */
  107. protected function isRequired($formId)
  108. {
  109. return (boolean)$this->getCaptchaModel($formId)->isRequired();
  110. }
  111. /**
  112. * Return captcha model for specified form
  113. *
  114. * @param string $formId
  115. * @return \Magento\Captcha\Model\CaptchaInterface
  116. */
  117. protected function getCaptchaModel($formId)
  118. {
  119. return $this->captchaData->getCaptcha($formId);
  120. }
  121. }