Automation.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model;
  3. class Automation extends \Magento\Framework\Model\AbstractModel
  4. {
  5. /**
  6. * @var ResourceModel\Automation
  7. */
  8. private $automationResource;
  9. /**
  10. * @var \Magento\Framework\Stdlib\DateTime
  11. */
  12. private $dateTime;
  13. /**
  14. * @var \Dotdigitalgroup\Email\Helper\Data
  15. */
  16. private $helper;
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface
  19. */
  20. private $storeManager;
  21. /**
  22. * Automation constructor.
  23. * @param \Magento\Framework\Model\Context $context
  24. * @param \Magento\Framework\Registry $registry
  25. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  26. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  27. * @param ResourceModel\Automation $automationResource
  28. * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
  29. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  30. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  31. * @param array $data
  32. */
  33. public function __construct(
  34. \Magento\Framework\Model\Context $context,
  35. \Magento\Framework\Registry $registry,
  36. \Magento\Framework\Stdlib\DateTime $dateTime,
  37. \Dotdigitalgroup\Email\Helper\Data $helper,
  38. \Dotdigitalgroup\Email\Model\ResourceModel\Automation $automationResource,
  39. \Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
  40. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  41. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  42. array $data = []
  43. ) {
  44. $this->dateTime = $dateTime;
  45. $this->automationResource = $automationResource;
  46. $this->helper = $helper;
  47. $this->storeManager = $storeManagerInterface;
  48. parent::__construct(
  49. $context,
  50. $registry,
  51. $resource,
  52. $resourceCollection,
  53. $data
  54. );
  55. }
  56. /**
  57. * Constructor.
  58. *
  59. * @return null
  60. */
  61. public function _construct()
  62. {
  63. parent::_construct();
  64. $this->_init(\Dotdigitalgroup\Email\Model\ResourceModel\Automation::class);
  65. }
  66. /**
  67. * Prepare data to be saved to database.
  68. *
  69. * @return $this
  70. */
  71. public function beforeSave()
  72. {
  73. parent::beforeSave();
  74. if ($this->isObjectNew()) {
  75. $this->setCreatedAt($this->dateTime->formatDate(true));
  76. }
  77. $this->setUpdatedAt($this->dateTime->formatDate(true));
  78. return $this;
  79. }
  80. /**
  81. * New customer automation
  82. *
  83. * @param \Magento\Customer\Model\Customer $customer
  84. *
  85. * @return null
  86. */
  87. public function newCustomerAutomation($customer)
  88. {
  89. $email = $customer->getEmail();
  90. $websiteId = $customer->getWebsiteId();
  91. $customerId = $customer->getId();
  92. $store = $this->storeManager->getStore($customer->getStoreId());
  93. $storeName = $store->getName();
  94. try {
  95. //Api is enabled
  96. $apiEnabled = $this->helper->getWebsiteConfig(
  97. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_API_ENABLED,
  98. $websiteId
  99. );
  100. //Automation enrolment
  101. $programId = $this->helper->getWebsiteConfig(
  102. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_AUTOMATION_STUDIO_CUSTOMER,
  103. $websiteId
  104. );
  105. //new contact program mapped
  106. if ($programId && $apiEnabled) {
  107. //save automation type
  108. $this->setEmail($email)
  109. ->setAutomationType(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_NEW_CUSTOMER)
  110. ->setEnrolmentStatus(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_STATUS_PENDING)
  111. ->setTypeId($customerId)
  112. ->setWebsiteId($websiteId)
  113. ->setStoreName($storeName)
  114. ->setProgramId($programId);
  115. $this->automationResource->save($this);
  116. }
  117. } catch (\Exception $e) {
  118. $this->helper->debug((string)$e, []);
  119. }
  120. }
  121. }