Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Importer.php
Go to the documentation of this file.
1 <?php
7 
16 
20 class Importer implements ImporterInterface
21 {
27  private $dataDifferenceCalculator;
28 
34  private $processFactory;
35 
41  private $storeManager;
42 
48  private $resource;
49 
55  private $cacheManager;
56 
64  public function __construct(
65  DataDifferenceCalculator $dataDifferenceCalculator,
66  ProcessorFactory $processFactory,
67  StoreManagerInterface $storeManager,
68  CacheInterface $cacheManager,
69  Website $resource
70  ) {
71  $this->dataDifferenceCalculator = $dataDifferenceCalculator;
72  $this->processFactory = $processFactory;
73  $this->storeManager = $storeManager;
74  $this->cacheManager = $cacheManager;
75  $this->resource = $resource;
76  }
77 
84  public function import(array $data)
85  {
86  $actions = [
90  ];
91  $messages = ['Stores were processed'];
92 
93  try {
94  $newGroups = $this->getGroupsToCreate($data);
95 
96  if ($newGroups) {
97  $messages[] = sprintf(
98  $this->getStoreGroupAssignMessage(),
99  implode(', ', array_column($newGroups, 'name'))
100  );
101  }
102  // during import websites/stores database already has new entities
103  // but cache is outdated which can cause to error in some cases
104  $this->reinitStores();
105 
106  $this->resource->beginTransaction();
107 
108  foreach ($actions as $action) {
109  $this->processFactory->create($action)->run($data);
110  }
111  } catch (\Exception $exception) {
112  $this->resource->rollBack();
113  $this->reinitStores();
114 
115  throw new InvalidTransitionException(__('%1', $exception->getMessage()), $exception);
116  }
117 
118  $this->resource->commit();
119  $this->reinitStores();
120 
121  return $messages;
122  }
123 
129  private function reinitStores()
130  {
131  $this->storeManager->reinitStores();
132  $this->cacheManager->clean();
133  }
134 
140  private function getStoreGroupAssignMessage()
141  {
142  return 'The following new store groups must be associated with a root category: %s. '
143  . PHP_EOL
144  . 'Associate a store group with a root category in the Admin Panel: Stores > Settings > All Stores.';
145  }
146 
153  private function getGroupsToCreate(array $data)
154  {
155  if (!isset($data[ScopeInterface::SCOPE_GROUPS])) {
156  return [];
157  }
158 
159  $groups = $this->dataDifferenceCalculator->getItemsToCreate(
162  );
163 
164  return $groups;
165  }
166 
172  public function getWarningMessages(array $data)
173  {
174  $messages = [];
175 
176  foreach ($data as $scope => $scopeData) {
177  $messageMap = [
178  'These %s will be deleted: %s' => $this->dataDifferenceCalculator->getItemsToDelete($scope, $scopeData),
179  'These %s will be updated: %s' => $this->dataDifferenceCalculator->getItemsToUpdate($scope, $scopeData),
180  'These %s will be created: %s' => $this->dataDifferenceCalculator->getItemsToCreate($scope, $scopeData),
181  ];
182 
183  foreach ($messageMap as $message => $items) {
184  if (!$items) {
185  continue;
186  }
187 
188  $messages[] = $this->formatMessage($message, $items, $scope);
189  }
190  }
191 
192  return $messages;
193  }
194 
203  private function formatMessage($message, array $items, $scope)
204  {
205  return sprintf(
206  $message,
207  ucfirst($scope),
208  implode(', ', array_column($items, 'name'))
209  );
210  }
211 }
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$message
__construct(DataDifferenceCalculator $dataDifferenceCalculator, ProcessorFactory $processFactory, StoreManagerInterface $storeManager, CacheInterface $cacheManager, Website $resource)
Definition: Importer.php:64
$items