Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IntegrationManager.php
Go to the documentation of this file.
1 <?php
8 
14 
21 {
25  private $config;
26 
30  private $integrationService;
31 
35  private $oauthService;
36 
44  public function __construct(
45  SystemConfig $config,
46  IntegrationServiceInterface $integrationService,
47  OauthServiceInterface $oauthService
48  ) {
49  $this->integrationService = $integrationService;
50  $this->config = $config;
51  $this->oauthService = $oauthService;
52  }
53 
60  public function activateIntegration()
61  {
62  $integration = $this->integrationService->findByName(
63  $this->config->getConfigDataValue('analytics/integration_name')
64  );
65  if (!$integration->getId()) {
66  throw new NoSuchEntityException(__('Cannot find predefined integration user!'));
67  }
68  $integrationData = $this->getIntegrationData(Integration::STATUS_ACTIVE);
69  $integrationData['integration_id'] = $integration->getId();
70  $this->integrationService->update($integrationData);
71  return true;
72  }
73 
79  public function generateToken()
80  {
81  $consumerId = $this->generateIntegration()->getConsumerId();
82  $accessToken = $this->oauthService->getAccessToken($consumerId);
83  if (!$accessToken && $this->oauthService->createAccessToken($consumerId, true)) {
84  $accessToken = $this->oauthService->getAccessToken($consumerId);
85  }
86  return $accessToken;
87  }
88 
94  private function generateIntegration()
95  {
96  $integration = $this->integrationService->findByName(
97  $this->config->getConfigDataValue('analytics/integration_name')
98  );
99  if (!$integration->getId()) {
100  $integration = $this->integrationService->create($this->getIntegrationData());
101  }
102  return $integration;
103  }
104 
111  private function getIntegrationData($status = Integration::STATUS_INACTIVE)
112  {
113  $integrationData = [
114  'name' => $this->config->getConfigDataValue('analytics/integration_name'),
115  'status' => $status,
116  'all_resources' => false,
117  'resource' => [
118  'Magento_Analytics::analytics',
119  'Magento_Analytics::analytics_api'
120  ],
121  ];
122  return $integrationData;
123  }
124 }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$status
Definition: order_status.php:8
__construct(SystemConfig $config, IntegrationServiceInterface $integrationService, OauthServiceInterface $oauthService)