Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
History.php
Go to the documentation of this file.
1 <?php
7 
17 {
18  const HISTORY_ID = 'history_id';
19 
20  const STARTED_AT = 'started_at';
21 
22  const USER_ID = 'user_id';
23 
24  const IMPORTED_FILE = 'imported_file';
25 
26  const ERROR_FILE = 'error_file';
27 
28  const EXECUTION_TIME = 'execution_time';
29 
30  const SUMMARY = 'summary';
31 
32  const IMPORT_IN_PROCESS = 'In Progress';
33 
34  const IMPORT_VALIDATION = 'Validation';
35 
36  const IMPORT_FAILED = 'Failed';
37 
39 
43  protected $reportHelper;
44 
56  public function __construct(
57  \Magento\Framework\Model\Context $context,
58  \Magento\Framework\Registry $registry,
60  \Magento\ImportExport\Model\ResourceModel\History\Collection $resourceCollection,
61  \Magento\ImportExport\Helper\Report $reportHelper,
62  \Magento\Backend\Model\Auth\Session $authSession,
63  array $data = []
64  ) {
65  $this->reportHelper = $reportHelper;
66  $this->session = $authSession;
67 
68  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
69  }
70 
76  protected function _construct()
77  {
78  $this->_init(\Magento\ImportExport\Model\ResourceModel\History::class);
79  }
80 
87  public function addReport($filename)
88  {
89  $this->setUserId($this->getAdminId());
90  $this->setExecutionTime(self::IMPORT_VALIDATION);
91  $this->setImportedFile($filename);
92  $this->save();
93  return $this;
94  }
95 
102  public function addErrorReportFile($filename)
103  {
104  $this->setErrorFile($filename);
105  $this->save();
106  return $this;
107  }
108 
116  public function updateReport(Import $import, $updateSummary = false)
117  {
118  if ($import->isReportEntityType()) {
119  $this->load($this->getLastItemId());
120  $executionResult = self::IMPORT_IN_PROCESS;
121  if ($updateSummary) {
122  $executionResult = $this->reportHelper->getExecutionTime($this->getStartedAt());
123  $summary = $this->reportHelper->getSummaryStats($import);
124  $this->setSummary($summary);
125  }
126  $this->setExecutionTime($executionResult);
127  $this->save();
128  }
129  return $this;
130  }
131 
138  public function invalidateReport(Import $import)
139  {
140  if ($import->isReportEntityType()) {
141  $this->load($this->getLastItemId());
142  $this->setExecutionTime(self::IMPORT_FAILED);
143  $this->save();
144  }
145  return $this;
146  }
147 
153  public function getId()
154  {
155  return $this->getData(self::HISTORY_ID);
156  }
157 
163  public function getStartedAt()
164  {
165  return $this->getData(self::STARTED_AT);
166  }
167 
173  public function getUserId()
174  {
175  return $this->getData(self::USER_ID);
176  }
177 
183  public function getImportedFile()
184  {
185  return $this->getData(self::IMPORTED_FILE);
186  }
187 
193  public function getErrorFile()
194  {
195  return $this->getData(self::ERROR_FILE);
196  }
197 
203  public function getExecutionTime()
204  {
205  return $this->getData(self::EXECUTION_TIME);
206  }
207 
213  public function getSummary()
214  {
215  return $this->getData(self::SUMMARY);
216  }
217 
224  public function setId($id)
225  {
226  return $this->setData(self::HISTORY_ID, $id);
227  }
228 
235  public function setStartedAt($startedAt)
236  {
237  return $this->setData(self::STARTED_AT, $startedAt);
238  }
239 
246  public function setUserId($userId)
247  {
248  return $this->setData(self::USER_ID, $userId);
249  }
250 
257  public function setImportedFile($importedFile)
258  {
259  return $this->setData(self::IMPORTED_FILE, $importedFile);
260  }
261 
268  public function setErrorFile($errorFile)
269  {
270  return $this->setData(self::ERROR_FILE, $errorFile);
271  }
272 
279  public function setExecutionTime($executionTime)
280  {
281  return $this->setData(self::EXECUTION_TIME, $executionTime);
282  }
283 
290  public function setSummary($summary)
291  {
292  return $this->setData(self::SUMMARY, $summary);
293  }
294 
298  public function loadLastInsertItem()
299  {
300  $this->load($this->getLastItemId());
301 
302  return $this;
303  }
304 
310  protected function getAdminId()
311  {
312  $userId = self::IMPORT_SCHEDULED_USER;
313  if ($this->session->isLoggedIn()) {
314  $userId = $this->session->getUser()->getId();
315  }
316  return $userId;
317  }
318 
324  protected function getLastItemId()
325  {
326  return $this->_resource->getLastInsertedId($this->getAdminId());
327  }
328 }
setExecutionTime($executionTime)
Definition: History.php:279
getData($key='', $index=null)
Definition: DataObject.php:119
isReportEntityType($entity=null)
Definition: Import.php:723
$id
Definition: fieldset.phtml:14
invalidateReport(Import $import)
Definition: History.php:138
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\ImportExport\Model\ResourceModel\History $resource, \Magento\ImportExport\Model\ResourceModel\History\Collection $resourceCollection, \Magento\ImportExport\Helper\Report $reportHelper, \Magento\Backend\Model\Auth\Session $authSession, array $data=[])
Definition: History.php:56
updateReport(Import $import, $updateSummary=false)
Definition: History.php:116