IntegrationService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model;
  7. use Magento\Integration\Model\Integration as IntegrationModel;
  8. use Magento\Integration\Model\IntegrationFactory;
  9. use Magento\Integration\Api\OauthServiceInterface as IntegrationOauthService;
  10. use Magento\Framework\Exception\IntegrationException;
  11. /**
  12. * Integration Service.
  13. *
  14. * This service is used to interact with integrations.
  15. */
  16. class IntegrationService implements \Magento\Integration\Api\IntegrationServiceInterface
  17. {
  18. /**
  19. * @var IntegrationFactory
  20. */
  21. protected $_integrationFactory;
  22. /**
  23. * @var IntegrationOauthService
  24. */
  25. protected $_oauthService;
  26. /**
  27. * Construct and initialize Integration Factory
  28. *
  29. * @param IntegrationFactory $integrationFactory
  30. * @param IntegrationOauthService $oauthService
  31. */
  32. public function __construct(IntegrationFactory $integrationFactory, IntegrationOauthService $oauthService)
  33. {
  34. $this->_integrationFactory = $integrationFactory;
  35. $this->_oauthService = $oauthService;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function create(array $integrationData)
  41. {
  42. $this->_checkIntegrationByName($integrationData['name']);
  43. $integration = $this->_integrationFactory->create()->setData($integrationData);
  44. $integration->save();
  45. $consumerName = 'Integration' . $integration->getId();
  46. $consumer = $this->_oauthService->createConsumer(['name' => $consumerName]);
  47. $integration->setConsumerId($consumer->getId());
  48. $integration->save();
  49. return $integration;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function update(array $integrationData)
  55. {
  56. $integration = $this->_loadIntegrationById($integrationData['integration_id']);
  57. //If name has been updated check if it conflicts with an existing integration
  58. if ($integration->getName() != $integrationData['name']) {
  59. $this->_checkIntegrationByName($integrationData['name']);
  60. }
  61. $integration->addData($integrationData);
  62. $integration->save();
  63. return $integration;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function delete($integrationId)
  69. {
  70. $integration = $this->_loadIntegrationById($integrationId);
  71. $data = $integration->getData();
  72. $integration->delete();
  73. return $data;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function get($integrationId)
  79. {
  80. $integration = $this->_loadIntegrationById($integrationId);
  81. $this->_addOauthConsumerData($integration);
  82. $this->_addOauthTokenData($integration);
  83. return $integration;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function findByName($name)
  89. {
  90. $integration = $this->_integrationFactory->create()->load($name, 'name');
  91. return $integration;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function findByConsumerId($consumerId)
  97. {
  98. $integration = $this->_integrationFactory->create()->load($consumerId, 'consumer_id');
  99. return $integration;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function findActiveIntegrationByConsumerId($consumerId)
  105. {
  106. $integration = $this->_integrationFactory->create()->loadActiveIntegrationByConsumerId($consumerId);
  107. return $integration;
  108. }
  109. /**
  110. * Check if an integration exists by the name
  111. *
  112. * @param string $name
  113. * @return void
  114. * @throws \Magento\Framework\Exception\IntegrationException
  115. */
  116. private function _checkIntegrationByName($name)
  117. {
  118. $integration = $this->_integrationFactory->create()->load($name, 'name');
  119. if ($integration->getId()) {
  120. throw new IntegrationException(__('The integration with name "%1" exists.', $name));
  121. }
  122. }
  123. /**
  124. * Load integration by id.
  125. *
  126. * @param int $integrationId
  127. * @return IntegrationModel
  128. * @throws \Magento\Framework\Exception\IntegrationException
  129. */
  130. protected function _loadIntegrationById($integrationId)
  131. {
  132. $integration = $this->_integrationFactory->create()->load($integrationId);
  133. if (!$integration->getId()) {
  134. throw new IntegrationException(__('The integration with ID "%1" doesn\'t exist.', $integrationId));
  135. }
  136. return $integration;
  137. }
  138. /**
  139. * Add oAuth consumer key and secret.
  140. *
  141. * @param IntegrationModel $integration
  142. * @return void
  143. */
  144. protected function _addOauthConsumerData(IntegrationModel $integration)
  145. {
  146. if ($integration->getId()) {
  147. $consumer = $this->_oauthService->loadConsumer($integration->getConsumerId());
  148. $integration->setData('consumer_key', $consumer->getKey());
  149. $integration->setData('consumer_secret', $consumer->getSecret());
  150. }
  151. }
  152. /**
  153. * Add oAuth token and token secret.
  154. *
  155. * @param IntegrationModel $integration
  156. * @return void
  157. */
  158. protected function _addOauthTokenData(IntegrationModel $integration)
  159. {
  160. if ($integration->getId()) {
  161. $accessToken = $this->_oauthService->getAccessToken($integration->getConsumerId());
  162. if ($accessToken) {
  163. $integration->setData('token', $accessToken->getToken());
  164. $integration->setData('token_secret', $accessToken->getSecret());
  165. }
  166. }
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getSelectedResources($integrationId)
  172. {
  173. $integration = $this->get($integrationId);
  174. $data = $integration->getData();
  175. $selectedResourceIds = [];
  176. if ($data && isset($data['resource']) && is_array($data['resource'])) {
  177. $selectedResourceIds = $data['resource'];
  178. }
  179. return $selectedResourceIds;
  180. }
  181. }