ConfigProvider.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Checkout;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Customer\Model\Url;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\Customer\Model\Form;
  14. use Magento\Store\Model\ScopeInterface;
  15. class ConfigProvider implements ConfigProviderInterface
  16. {
  17. /**
  18. * @var StoreManagerInterface
  19. */
  20. protected $storeManager;
  21. /**
  22. * @var UrlInterface
  23. * @deprecated 101.0.4
  24. */
  25. protected $urlBuilder;
  26. /**
  27. * @var ScopeConfigInterface
  28. */
  29. protected $scopeConfig;
  30. /**
  31. * @var Url
  32. */
  33. private $customerUrl;
  34. /**
  35. * @param UrlInterface $urlBuilder
  36. * @param StoreManagerInterface $storeManager
  37. * @param ScopeConfigInterface $scopeConfig
  38. * @param Url|null $customerUrl
  39. */
  40. public function __construct(
  41. UrlInterface $urlBuilder,
  42. StoreManagerInterface $storeManager,
  43. ScopeConfigInterface $scopeConfig,
  44. Url $customerUrl = null
  45. ) {
  46. $this->urlBuilder = $urlBuilder;
  47. $this->storeManager = $storeManager;
  48. $this->scopeConfig = $scopeConfig;
  49. $this->customerUrl = $customerUrl ?? ObjectManager::getInstance()
  50. ->get(Url::class);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getConfig()
  56. {
  57. return [
  58. 'customerLoginUrl' => $this->getLoginUrl(),
  59. 'isRedirectRequired' => $this->isRedirectRequired(),
  60. 'autocomplete' => $this->isAutocompleteEnabled(),
  61. ];
  62. }
  63. /**
  64. * Is autocomplete enabled for storefront
  65. *
  66. * @return string
  67. * @codeCoverageIgnore
  68. */
  69. protected function isAutocompleteEnabled()
  70. {
  71. return $this->scopeConfig->getValue(
  72. Form::XML_PATH_ENABLE_AUTOCOMPLETE,
  73. ScopeInterface::SCOPE_STORE
  74. ) ? 'on' : 'off';
  75. }
  76. /**
  77. * Returns URL to login controller action
  78. *
  79. * @return string
  80. */
  81. protected function getLoginUrl()
  82. {
  83. return $this->customerUrl->getLoginUrl();
  84. }
  85. /**
  86. * Whether redirect to login page is required
  87. *
  88. * @return bool
  89. */
  90. protected function isRedirectRequired()
  91. {
  92. $baseUrl = $this->storeManager->getStore()->getBaseUrl();
  93. if (strpos($this->getLoginUrl(), $baseUrl) !== false) {
  94. return false;
  95. }
  96. return true;
  97. }
  98. }