Importer.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel;
  3. use Dotdigitalgroup\Email\Setup\Schema;
  4. class Importer extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  5. {
  6. /**
  7. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  8. */
  9. private $localeDate;
  10. /**
  11. * @var \Dotdigitalgroup\Email\Model\DateIntervalFactory
  12. */
  13. private $dateIntervalFactory;
  14. /**
  15. * Importer constructor.
  16. *
  17. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  18. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  19. * @param \Dotdigitalgroup\Email\Model\DateIntervalFactory $dateIntervalFactory
  20. */
  21. public function __construct(
  22. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  23. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  24. \Dotdigitalgroup\Email\Model\DateIntervalFactory $dateIntervalFactory
  25. ) {
  26. $this->dateIntervalFactory = $dateIntervalFactory;
  27. $this->localeDate = $localeDate;
  28. parent::__construct($context);
  29. }
  30. /**
  31. * Initialize resource.
  32. *
  33. * @return null
  34. */
  35. public function _construct()
  36. {
  37. $this->_init(Schema::EMAIL_IMPORTER_TABLE, 'id');
  38. }
  39. /**
  40. * Reset importer items.
  41. *
  42. * @param array $ids
  43. *
  44. * @return int|string
  45. */
  46. public function massResend($ids)
  47. {
  48. try {
  49. $conn = $this->getConnection();
  50. $num = $conn->update(
  51. $this->getTable(Schema::EMAIL_IMPORTER_TABLE),
  52. ['import_status' => 0],
  53. ['id IN(?)' => $ids]
  54. );
  55. return $num;
  56. } catch (\Exception $e) {
  57. return $e->getMessage();
  58. }
  59. }
  60. /**
  61. * Delete completed records older then 30 days from provided table.
  62. *
  63. * @param string $tableName
  64. *
  65. * @return \Exception|int
  66. */
  67. public function cleanup($tableName)
  68. {
  69. try {
  70. $interval = $this->dateIntervalFactory->create(['interval_spec' => 'P30D']);
  71. $date = $this->localeDate->date()->sub($interval)->format('Y-m-d H:i:s');
  72. $conn = $this->getConnection();
  73. $num = $conn->delete(
  74. $this->getTable($tableName),
  75. ['created_at < ?' => $date]
  76. );
  77. return $num;
  78. } catch (\Exception $e) {
  79. return $e->getMessage();
  80. }
  81. }
  82. /**
  83. * Save item
  84. *
  85. * @param \Dotdigitalgroup\Email\Model\\Importer $item
  86. *
  87. * @return $this
  88. */
  89. public function saveItem($item)
  90. {
  91. return $this->save($item);
  92. }
  93. }