History.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ImportExport\Model;
  7. /**
  8. * Import history model
  9. *
  10. * @api
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. * @SuppressWarnings(PHPMD.LongVariable)
  13. * @since 100.0.2
  14. */
  15. class History extends \Magento\Framework\Model\AbstractModel
  16. {
  17. const HISTORY_ID = 'history_id';
  18. const STARTED_AT = 'started_at';
  19. const USER_ID = 'user_id';
  20. const IMPORTED_FILE = 'imported_file';
  21. const ERROR_FILE = 'error_file';
  22. const EXECUTION_TIME = 'execution_time';
  23. const SUMMARY = 'summary';
  24. const IMPORT_IN_PROCESS = 'In Progress';
  25. const IMPORT_VALIDATION = 'Validation';
  26. const IMPORT_FAILED = 'Failed';
  27. const IMPORT_SCHEDULED_USER = 0;
  28. /**
  29. * @var \Magento\ImportExport\Helper\Report
  30. */
  31. protected $reportHelper;
  32. /**
  33. * @var \Magento\Backend\Model\Auth\Session
  34. * @since 100.3.1
  35. */
  36. protected $session;
  37. /**
  38. * Class constructor
  39. *
  40. * @param \Magento\Framework\Model\Context $context
  41. * @param \Magento\Framework\Registry $registry
  42. * @param \Magento\ImportExport\Model\ResourceModel\History $resource
  43. * @param \Magento\ImportExport\Model\ResourceModel\History\Collection $resourceCollection
  44. * @param \Magento\ImportExport\Helper\Report $reportHelper
  45. * @param \Magento\Backend\Model\Auth\Session $authSession
  46. * @param array $data
  47. */
  48. public function __construct(
  49. \Magento\Framework\Model\Context $context,
  50. \Magento\Framework\Registry $registry,
  51. \Magento\ImportExport\Model\ResourceModel\History $resource,
  52. \Magento\ImportExport\Model\ResourceModel\History\Collection $resourceCollection,
  53. \Magento\ImportExport\Helper\Report $reportHelper,
  54. \Magento\Backend\Model\Auth\Session $authSession,
  55. array $data = []
  56. ) {
  57. $this->reportHelper = $reportHelper;
  58. $this->session = $authSession;
  59. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  60. }
  61. /**
  62. * Initialize history resource model
  63. *
  64. * @return void
  65. */
  66. protected function _construct()
  67. {
  68. $this->_init(\Magento\ImportExport\Model\ResourceModel\History::class);
  69. }
  70. /**
  71. * Add import history report
  72. *
  73. * @param string $filename
  74. * @return $this
  75. */
  76. public function addReport($filename)
  77. {
  78. $this->setUserId($this->getAdminId());
  79. $this->setExecutionTime(self::IMPORT_VALIDATION);
  80. $this->setImportedFile($filename);
  81. $this->save();
  82. return $this;
  83. }
  84. /**
  85. * Add errors to import history report
  86. *
  87. * @param string $filename
  88. * @return $this
  89. */
  90. public function addErrorReportFile($filename)
  91. {
  92. $this->setErrorFile($filename);
  93. $this->save();
  94. return $this;
  95. }
  96. /**
  97. * Update import history report
  98. *
  99. * @param Import $import
  100. * @param bool $updateSummary
  101. * @return $this
  102. */
  103. public function updateReport(Import $import, $updateSummary = false)
  104. {
  105. if ($import->isReportEntityType()) {
  106. $this->load($this->getLastItemId());
  107. $executionResult = self::IMPORT_IN_PROCESS;
  108. if ($updateSummary) {
  109. $executionResult = $this->reportHelper->getExecutionTime($this->getStartedAt());
  110. $summary = $this->reportHelper->getSummaryStats($import);
  111. $this->setSummary($summary);
  112. }
  113. $this->setExecutionTime($executionResult);
  114. $this->save();
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Mark history report as invalid
  120. *
  121. * @param Import $import
  122. * @return $this
  123. */
  124. public function invalidateReport(Import $import)
  125. {
  126. if ($import->isReportEntityType()) {
  127. $this->load($this->getLastItemId());
  128. $this->setExecutionTime(self::IMPORT_FAILED);
  129. $this->save();
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Get import history report ID
  135. *
  136. * @return string
  137. */
  138. public function getId()
  139. {
  140. return $this->getData(self::HISTORY_ID);
  141. }
  142. /**
  143. * Get import history report ID
  144. *
  145. * @return string
  146. */
  147. public function getStartedAt()
  148. {
  149. return $this->getData(self::STARTED_AT);
  150. }
  151. /**
  152. * Get import history report ID
  153. *
  154. * @return string
  155. */
  156. public function getUserId()
  157. {
  158. return $this->getData(self::USER_ID);
  159. }
  160. /**
  161. * Get imported file
  162. *
  163. * @return string
  164. */
  165. public function getImportedFile()
  166. {
  167. return $this->getData(self::IMPORTED_FILE);
  168. }
  169. /**
  170. * Get error file
  171. *
  172. * @return string
  173. */
  174. public function getErrorFile()
  175. {
  176. return $this->getData(self::ERROR_FILE);
  177. }
  178. /**
  179. * Get import execution time
  180. *
  181. * @return string
  182. */
  183. public function getExecutionTime()
  184. {
  185. return $this->getData(self::EXECUTION_TIME);
  186. }
  187. /**
  188. * Get import history report summary
  189. *
  190. * @return string
  191. */
  192. public function getSummary()
  193. {
  194. return $this->getData(self::SUMMARY);
  195. }
  196. /**
  197. * Set history report ID
  198. *
  199. * @param int $id
  200. * @return $this
  201. */
  202. public function setId($id)
  203. {
  204. return $this->setData(self::HISTORY_ID, $id);
  205. }
  206. /**
  207. * Set history report starting time
  208. *
  209. * @param string $startedAt
  210. * @return $this
  211. */
  212. public function setStartedAt($startedAt)
  213. {
  214. return $this->setData(self::STARTED_AT, $startedAt);
  215. }
  216. /**
  217. * Set user id
  218. *
  219. * @param int $userId
  220. * @return $this
  221. */
  222. public function setUserId($userId)
  223. {
  224. return $this->setData(self::USER_ID, $userId);
  225. }
  226. /**
  227. * Set imported file name
  228. *
  229. * @param string $importedFile
  230. * @return $this
  231. */
  232. public function setImportedFile($importedFile)
  233. {
  234. return $this->setData(self::IMPORTED_FILE, $importedFile);
  235. }
  236. /**
  237. * Set error file name
  238. *
  239. * @param string $errorFile
  240. * @return $this
  241. */
  242. public function setErrorFile($errorFile)
  243. {
  244. return $this->setData(self::ERROR_FILE, $errorFile);
  245. }
  246. /**
  247. * Set Execution Time
  248. *
  249. * @param string $executionTime
  250. * @return $this
  251. */
  252. public function setExecutionTime($executionTime)
  253. {
  254. return $this->setData(self::EXECUTION_TIME, $executionTime);
  255. }
  256. /**
  257. * Set summary
  258. *
  259. * @param string $summary
  260. * @return $this
  261. */
  262. public function setSummary($summary)
  263. {
  264. return $this->setData(self::SUMMARY, $summary);
  265. }
  266. /**
  267. * Load the last inserted item
  268. *
  269. * @return $this
  270. */
  271. public function loadLastInsertItem()
  272. {
  273. $this->load($this->getLastItemId());
  274. return $this;
  275. }
  276. /**
  277. * Retrieve admin ID
  278. *
  279. * @return string
  280. */
  281. protected function getAdminId()
  282. {
  283. $userId = self::IMPORT_SCHEDULED_USER;
  284. if ($this->session->isLoggedIn()) {
  285. $userId = $this->session->getUser()->getId();
  286. }
  287. return $userId;
  288. }
  289. /**
  290. * Retrieve last history report ID
  291. *
  292. * @return string
  293. */
  294. protected function getLastItemId()
  295. {
  296. return $this->_resource->getLastInsertedId($this->getAdminId());
  297. }
  298. }