IntegrationManager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Integration\Api\IntegrationServiceInterface;
  9. use Magento\Config\Model\Config as SystemConfig;
  10. use Magento\Integration\Model\Integration;
  11. use Magento\Integration\Api\OauthServiceInterface;
  12. /**
  13. * Manages the integration user at magento side.
  14. * User name stored in config.
  15. * User roles
  16. */
  17. class IntegrationManager
  18. {
  19. /**
  20. * @var SystemConfig
  21. */
  22. private $config;
  23. /**
  24. * @var IntegrationServiceInterface
  25. */
  26. private $integrationService;
  27. /**
  28. * @var OauthServiceInterface
  29. */
  30. private $oauthService;
  31. /**
  32. * IntegrationManager constructor
  33. *
  34. * @param SystemConfig $config
  35. * @param IntegrationServiceInterface $integrationService
  36. * @param OauthServiceInterface $oauthService
  37. */
  38. public function __construct(
  39. SystemConfig $config,
  40. IntegrationServiceInterface $integrationService,
  41. OauthServiceInterface $oauthService
  42. ) {
  43. $this->integrationService = $integrationService;
  44. $this->config = $config;
  45. $this->oauthService = $oauthService;
  46. }
  47. /**
  48. * Activate predefined integration user
  49. *
  50. * @return bool
  51. * @throws NoSuchEntityException
  52. */
  53. public function activateIntegration()
  54. {
  55. $integration = $this->integrationService->findByName(
  56. $this->config->getConfigDataValue('analytics/integration_name')
  57. );
  58. if (!$integration->getId()) {
  59. throw new NoSuchEntityException(__('Cannot find predefined integration user!'));
  60. }
  61. $integrationData = $this->getIntegrationData(Integration::STATUS_ACTIVE);
  62. $integrationData['integration_id'] = $integration->getId();
  63. $this->integrationService->update($integrationData);
  64. return true;
  65. }
  66. /**
  67. * This method execute Generate Token command and enable integration
  68. *
  69. * @return bool|\Magento\Integration\Model\Oauth\Token
  70. */
  71. public function generateToken()
  72. {
  73. $consumerId = $this->generateIntegration()->getConsumerId();
  74. $accessToken = $this->oauthService->getAccessToken($consumerId);
  75. if (!$accessToken && $this->oauthService->createAccessToken($consumerId, true)) {
  76. $accessToken = $this->oauthService->getAccessToken($consumerId);
  77. }
  78. return $accessToken;
  79. }
  80. /**
  81. * Returns consumer Id for MA integration user
  82. *
  83. * @return \Magento\Integration\Model\Integration
  84. */
  85. private function generateIntegration()
  86. {
  87. $integration = $this->integrationService->findByName(
  88. $this->config->getConfigDataValue('analytics/integration_name')
  89. );
  90. if (!$integration->getId()) {
  91. $integration = $this->integrationService->create($this->getIntegrationData());
  92. }
  93. return $integration;
  94. }
  95. /**
  96. * Returns default attributes for MA integration user
  97. *
  98. * @param int $status
  99. * @return array
  100. */
  101. private function getIntegrationData($status = Integration::STATUS_INACTIVE)
  102. {
  103. $integrationData = [
  104. 'name' => $this->config->getConfigDataValue('analytics/integration_name'),
  105. 'status' => $status,
  106. 'all_resources' => false,
  107. 'resource' => [
  108. 'Magento_Analytics::analytics',
  109. 'Magento_Analytics::analytics_api'
  110. ],
  111. ];
  112. return $integrationData;
  113. }
  114. }