Integration.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Controller\Adminhtml;
  7. use Magento\Backend\App\Action;
  8. use Magento\Integration\Api\OauthServiceInterface as IntegrationOauthService;
  9. /**
  10. * Controller for integrations management.
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. abstract class Integration extends Action
  15. {
  16. /**
  17. * Authorization level of a basic admin session
  18. *
  19. * @see _isAllowed()
  20. */
  21. const ADMIN_RESOURCE = 'Magento_Integration::integrations';
  22. /** Param Key for extracting integration id from Request */
  23. const PARAM_INTEGRATION_ID = 'id';
  24. /** Reauthorize flag is used to distinguish activation from reauthorization */
  25. const PARAM_REAUTHORIZE = 'reauthorize';
  26. const REGISTRY_KEY_CURRENT_INTEGRATION = 'current_integration';
  27. /** Saved API form data session key */
  28. const REGISTRY_KEY_CURRENT_RESOURCE = 'current_resource';
  29. /**
  30. * @var \Magento\Framework\Registry
  31. */
  32. protected $_registry;
  33. /**
  34. * @var \Psr\Log\LoggerInterface
  35. */
  36. protected $_logger;
  37. /**
  38. * @var \Magento\Integration\Api\IntegrationServiceInterface
  39. */
  40. protected $_integrationService;
  41. /**
  42. * @var \Magento\Integration\Api\OauthServiceInterface
  43. */
  44. protected $_oauthService;
  45. /**
  46. * @var \Magento\Framework\Json\Helper\Data
  47. */
  48. protected $jsonHelper;
  49. /**
  50. * @var \Magento\Integration\Helper\Data
  51. */
  52. protected $_integrationData;
  53. /**
  54. * @var \Magento\Integration\Model\ResourceModel\Integration\Collection
  55. */
  56. protected $_integrationCollection;
  57. /**
  58. * @var \Magento\Framework\Escaper
  59. */
  60. protected $escaper;
  61. /**
  62. * @param \Magento\Backend\App\Action\Context $context
  63. * @param \Magento\Framework\Registry $registry
  64. * @param \Psr\Log\LoggerInterface $logger
  65. * @param \Magento\Integration\Api\IntegrationServiceInterface $integrationService
  66. * @param \Magento\Integration\Api\OauthServiceInterface $oauthService
  67. * @param \Magento\Framework\Json\Helper\Data $jsonHelper
  68. * @param \Magento\Integration\Helper\Data $integrationData
  69. * @param \Magento\Framework\Escaper $escaper
  70. * @param \Magento\Integration\Model\ResourceModel\Integration\Collection $integrationCollection
  71. */
  72. public function __construct(
  73. \Magento\Backend\App\Action\Context $context,
  74. \Magento\Framework\Registry $registry,
  75. \Psr\Log\LoggerInterface $logger,
  76. \Magento\Integration\Api\IntegrationServiceInterface $integrationService,
  77. \Magento\Integration\Api\OauthServiceInterface $oauthService,
  78. \Magento\Framework\Json\Helper\Data $jsonHelper,
  79. \Magento\Integration\Helper\Data $integrationData,
  80. \Magento\Framework\Escaper $escaper,
  81. \Magento\Integration\Model\ResourceModel\Integration\Collection $integrationCollection
  82. ) {
  83. parent::__construct($context);
  84. $this->_registry = $registry;
  85. $this->_logger = $logger;
  86. $this->_integrationService = $integrationService;
  87. $this->_oauthService = $oauthService;
  88. $this->jsonHelper = $jsonHelper;
  89. $this->_integrationData = $integrationData;
  90. $this->escaper = $escaper;
  91. $this->_integrationCollection = $integrationCollection;
  92. parent::__construct($context);
  93. }
  94. /**
  95. * Don't actually redirect if we've got AJAX request - return redirect URL instead.
  96. *
  97. * @param string $path
  98. * @param array $arguments
  99. * @return $this|\Magento\Backend\App\AbstractAction
  100. */
  101. protected function _redirect($path, $arguments = [])
  102. {
  103. if ($this->getRequest()->isXmlHttpRequest()) {
  104. $this->getResponse()->representJson(
  105. $this->jsonHelper->jsonEncode(['_redirect' => $this->getUrl($path, $arguments)])
  106. );
  107. return $this;
  108. } else {
  109. return parent::_redirect($path, $arguments);
  110. }
  111. }
  112. /**
  113. * Restore saved form resources
  114. *
  115. * @return void
  116. */
  117. protected function restoreResourceAndSaveToRegistry()
  118. {
  119. $restoredFormData = $this->_getSession()->getIntegrationData();
  120. if ($restoredFormData) {
  121. $resource = isset($restoredFormData['resource']) ? $restoredFormData['resource'] : [];
  122. $this->_registry->register(
  123. self::REGISTRY_KEY_CURRENT_RESOURCE,
  124. ['all_resources' => $restoredFormData['all_resources'], 'resource' => $resource]
  125. );
  126. }
  127. }
  128. }