Data.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Helper\Oauth;
  7. /**
  8. * OAuth View Helper for Controllers
  9. */
  10. class Data
  11. {
  12. /**
  13. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  14. */
  15. protected $_scopeConfig;
  16. /**
  17. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  18. */
  19. public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
  20. {
  21. $this->_scopeConfig = $scopeConfig;
  22. }
  23. /**#@+
  24. * Cleanup xpath config settings
  25. */
  26. const XML_PATH_CLEANUP_PROBABILITY = 'oauth/cleanup/cleanup_probability';
  27. const XML_PATH_CLEANUP_EXPIRATION_PERIOD = 'oauth/cleanup/expiration_period';
  28. /**#@-*/
  29. /**
  30. * Cleanup expiration period in minutes
  31. */
  32. const CLEANUP_EXPIRATION_PERIOD_DEFAULT = 120;
  33. /**#@+
  34. * Consumer xpath settings
  35. */
  36. const XML_PATH_CONSUMER_EXPIRATION_PERIOD = 'oauth/consumer/expiration_period';
  37. const XML_PATH_CONSUMER_POST_MAXREDIRECTS = 'oauth/consumer/post_maxredirects';
  38. const XML_PATH_CONSUMER_POST_TIMEOUT = 'oauth/consumer/post_timeout';
  39. /**#@-*/
  40. /**#@+
  41. * Consumer default settings
  42. */
  43. const CONSUMER_EXPIRATION_PERIOD_DEFAULT = 300;
  44. const CONSUMER_POST_TIMEOUT_DEFAULT = 5;
  45. /**#@-*/
  46. /**
  47. * Calculate cleanup possibility for data with lifetime property
  48. *
  49. * @return bool
  50. */
  51. public function isCleanupProbability()
  52. {
  53. // Safe get cleanup probability value from system configuration
  54. $configValue = (int)$this->_scopeConfig->getValue(self::XML_PATH_CLEANUP_PROBABILITY);
  55. return $configValue > 0 ? 1 == \Magento\Framework\Math\Random::getRandomNumber(1, $configValue) : false;
  56. }
  57. /**
  58. * Get cleanup expiration period value from system configuration in minutes
  59. *
  60. * @return int
  61. */
  62. public function getCleanupExpirationPeriod()
  63. {
  64. $minutes = (int)$this->_scopeConfig->getValue(self::XML_PATH_CLEANUP_EXPIRATION_PERIOD);
  65. return $minutes > 0 ? $minutes : self::CLEANUP_EXPIRATION_PERIOD_DEFAULT;
  66. }
  67. /**
  68. * Get consumer expiration period value from system configuration in seconds
  69. *
  70. * @return int
  71. */
  72. public function getConsumerExpirationPeriod()
  73. {
  74. $seconds = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_EXPIRATION_PERIOD);
  75. return $seconds > 0 ? $seconds : self::CONSUMER_EXPIRATION_PERIOD_DEFAULT;
  76. }
  77. /**
  78. * Get the number of consumer post maximum redirects
  79. *
  80. * @return int
  81. */
  82. public function getConsumerPostMaxRedirects()
  83. {
  84. $redirects = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_POST_MAXREDIRECTS);
  85. return $redirects > 0 ? $redirects : 0;
  86. }
  87. /**
  88. * Get the number seconds for the consumer post timeout
  89. *
  90. * @return int
  91. */
  92. public function getConsumerPostTimeout()
  93. {
  94. $seconds = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_POST_TIMEOUT);
  95. return $seconds > 0 ? $seconds : self::CONSUMER_POST_TIMEOUT_DEFAULT;
  96. }
  97. /**
  98. * Get customer token lifetime from config.
  99. *
  100. * @return int hours
  101. */
  102. public function getCustomerTokenLifetime()
  103. {
  104. $hours = (int)$this->_scopeConfig->getValue('oauth/access_token_lifetime/customer');
  105. return $hours > 0 ? $hours : 0;
  106. }
  107. /**
  108. * Get customer token lifetime from config.
  109. *
  110. * @return int hours
  111. */
  112. public function getAdminTokenLifetime()
  113. {
  114. $hours = (int)$this->_scopeConfig->getValue('oauth/access_token_lifetime/admin');
  115. return $hours > 0 ? $hours : 0;
  116. }
  117. }