Config.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\NewRelicReporting\Model;
  7. /**
  8. * NewRelic configuration model
  9. */
  10. class Config
  11. {
  12. /**#@+
  13. * Names of parameters to be sent to database tables
  14. */
  15. const ORDER_ITEMS = 'lineItemCount';
  16. const ORDER_VALUE = 'orderValue';
  17. const ORDER_PLACED = 'Order';
  18. const ADMIN_USER_ID = 'adminId';
  19. const ADMIN_USER = 'adminUser';
  20. const ADMIN_NAME = 'adminName';
  21. const CUSTOMER_ID = 'customerId';
  22. const CUSTOMER_NAME = 'CustomerName';
  23. const CUSTOMER_COUNT = 'CustomerCount';
  24. const FLUSH_CACHE = 'systemCacheFlush';
  25. const STORE = 'store';
  26. const STORE_VIEW_COUNT = 'StoreViewCount';
  27. const WEBSITE = 'website';
  28. const WEBSITE_COUNT = 'WebsiteCount';
  29. const PRODUCT_CHANGE = 'adminProductChange';
  30. const PRODUCT_COUNT = 'productCatalogSize';
  31. const CONFIGURABLE_COUNT = 'productCatalogConfigurableSize';
  32. const ACTIVE_COUNT = 'productCatalogActiveSize';
  33. const CATEGORY_SIZE = 'productCatalogCategorySize';
  34. const CATEGORY_COUNT = 'CatalogCategoryCount';
  35. const ENABLED_MODULE_COUNT = 'enabledModuleCount';
  36. const MODULES_ENABLED = 'ModulesEnabled';
  37. const MODULES_DISABLED = 'ModulesDisabled';
  38. const MODULES_INSTALLED = 'ModulesInstalled';
  39. const MODULE_INSTALLED = 'moduleInstalled';
  40. const MODULE_UNINSTALLED = 'moduleUninstalled';
  41. const MODULE_ENABLED = 'moduleEnabled';
  42. const MODULE_DISABLED = 'moduleDisabled';
  43. /**#@-*/
  44. /**#@+
  45. * Text flags for states
  46. */
  47. const INSTALLED = 'installed';
  48. const UNINSTALLED = 'uninstalled';
  49. const ENABLED = 'enabled';
  50. const DISABLED = 'disabled';
  51. const TRUE = 'true';
  52. const FALSE = 'false';
  53. /**#@-*/
  54. /**#@-*/
  55. protected $scopeConfig;
  56. /**
  57. * @var \Magento\Framework\Encryption\EncryptorInterface
  58. */
  59. protected $encryptor;
  60. /**
  61. * @var \Magento\Config\Model\ResourceModel\Config
  62. */
  63. protected $resourceConfig;
  64. /**
  65. * Constructor
  66. *
  67. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  68. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  69. * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig
  70. */
  71. public function __construct(
  72. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  73. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  74. \Magento\Config\Model\ResourceModel\Config $resourceConfig
  75. ) {
  76. $this->scopeConfig = $scopeConfig;
  77. $this->encryptor = $encryptor;
  78. $this->resourceConfig = $resourceConfig;
  79. }
  80. /**
  81. * Returns module's enabled status
  82. *
  83. * @return bool
  84. */
  85. public function isNewRelicEnabled()
  86. {
  87. return $this->scopeConfig->isSetFlag('newrelicreporting/general/enable');
  88. }
  89. /**
  90. * Returns configured URL for API
  91. *
  92. * @return string
  93. */
  94. public function getNewRelicApiUrl()
  95. {
  96. return (string)$this->scopeConfig->getValue('newrelicreporting/general/api_url');
  97. }
  98. /**
  99. * Returns configured URL for Insights API
  100. *
  101. * @return string
  102. */
  103. public function getInsightsApiUrl()
  104. {
  105. return (string)$this->scopeConfig->getValue('newrelicreporting/general/insights_api_url');
  106. }
  107. /**
  108. * Returns configured account ID for New Relic
  109. *
  110. * @return string
  111. */
  112. public function getNewRelicAccountId()
  113. {
  114. return (string)$this->scopeConfig->getValue('newrelicreporting/general/account_id');
  115. }
  116. /**
  117. * Return configured NR Application ID
  118. *
  119. * @return int
  120. */
  121. public function getNewRelicAppId()
  122. {
  123. return (int)$this->scopeConfig->getValue('newrelicreporting/general/app_id');
  124. }
  125. /**
  126. * Returns configured API key for APM
  127. *
  128. * @return string
  129. */
  130. public function getNewRelicApiKey()
  131. {
  132. return $this->encryptor->decrypt($this->scopeConfig->getValue('newrelicreporting/general/api'));
  133. }
  134. /**
  135. * Returns configured Insights insert key for New Relic events related to cron jobs
  136. *
  137. * @return string
  138. */
  139. public function getInsightsInsertKey()
  140. {
  141. return $this->encryptor->decrypt($this->scopeConfig->getValue('newrelicreporting/general/insights_insert_key'));
  142. }
  143. /**
  144. * Returns configured NR Application name
  145. *
  146. * @return string
  147. */
  148. public function getNewRelicAppName()
  149. {
  150. return (string)$this->scopeConfig->getValue('newrelicreporting/general/app_name');
  151. }
  152. /**
  153. * Returns configured separate apps value
  154. *
  155. * @return bool
  156. */
  157. public function isSeparateApps()
  158. {
  159. return (bool)$this->scopeConfig->getValue('newrelicreporting/general/separate_apps');
  160. }
  161. /**
  162. * Returns config setting for overall cron to be enabled
  163. *
  164. * @return bool
  165. */
  166. public function isCronEnabled()
  167. {
  168. return $this->scopeConfig->isSetFlag('newrelicreporting/cron/enable_cron');
  169. }
  170. /**
  171. * Sets config value
  172. *
  173. * @param string $pathId
  174. * @param mixed $value
  175. * @param string $scope
  176. * @param int $scopeId
  177. * @return void
  178. */
  179. protected function setConfigValue($pathId, $value, $scope = 'default', $scopeId = 0)
  180. {
  181. $this->resourceConfig->saveConfig($pathId, $value, $scope, $scopeId);
  182. }
  183. /**
  184. * Disable module's functionality for case when new relic extension is not available
  185. *
  186. * @return void
  187. */
  188. public function disableModule()
  189. {
  190. $this->setConfigValue('newrelicreporting/general/enable', 0);
  191. }
  192. }