Subscriber.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model\ResourceModel;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Store\Model\StoreManagerInterface;
  9. /**
  10. * Newsletter subscriber resource model
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Subscriber extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  18. {
  19. /**
  20. * DB connection
  21. *
  22. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  23. */
  24. protected $connection;
  25. /**
  26. * Name of subscriber link DB table
  27. *
  28. * @var string
  29. */
  30. protected $_subscriberLinkTable;
  31. /**
  32. * Name of scope for error messages
  33. *
  34. * @var string
  35. */
  36. protected $_messagesScope = 'newsletter/session';
  37. /**
  38. * Date
  39. *
  40. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  41. */
  42. protected $_date;
  43. /**
  44. * @var \Magento\Framework\Math\Random
  45. */
  46. protected $mathRandom;
  47. /**
  48. * Store manager
  49. *
  50. * @var StoreManagerInterface
  51. */
  52. private $storeManager;
  53. /**
  54. * Construct
  55. *
  56. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  57. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  58. * @param \Magento\Framework\Math\Random $mathRandom
  59. * @param string $connectionName
  60. * @param StoreManagerInterface $storeManager
  61. */
  62. public function __construct(
  63. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  64. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  65. \Magento\Framework\Math\Random $mathRandom,
  66. $connectionName = null,
  67. StoreManagerInterface $storeManager = null
  68. ) {
  69. $this->_date = $date;
  70. $this->mathRandom = $mathRandom;
  71. $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
  72. parent::__construct($context, $connectionName);
  73. }
  74. /**
  75. * Initialize resource model. Get tablename from config
  76. *
  77. * @return void
  78. */
  79. protected function _construct()
  80. {
  81. $this->_init('newsletter_subscriber', 'subscriber_id');
  82. $this->_subscriberLinkTable = $this->getTable('newsletter_queue_link');
  83. $this->connection = $this->getConnection();
  84. }
  85. /**
  86. * Set error messages scope
  87. *
  88. * @param string $scope
  89. * @return void
  90. */
  91. public function setMessagesScope($scope)
  92. {
  93. $this->_messagesScope = $scope;
  94. }
  95. /**
  96. * Load subscriber from DB by email
  97. *
  98. * @param string $subscriberEmail
  99. * @return array
  100. */
  101. public function loadByEmail($subscriberEmail)
  102. {
  103. $select = $this->connection->select()->from($this->getMainTable())->where('subscriber_email=:subscriber_email');
  104. $result = $this->connection->fetchRow($select, ['subscriber_email' => $subscriberEmail]);
  105. if (!$result) {
  106. return [];
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Load subscriber by customer
  112. *
  113. * @param \Magento\Customer\Api\Data\CustomerInterface $customer
  114. * @return array
  115. */
  116. public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
  117. {
  118. $storeIds = $this->storeManager->getWebsite($customer->getWebsiteId())->getStoreIds();
  119. if ($customer->getId()) {
  120. $select = $this->connection
  121. ->select()
  122. ->from($this->getMainTable())
  123. ->where('customer_id = ?', $customer->getId())
  124. ->where('store_id IN (?)', $storeIds)
  125. ->limit(1);
  126. $result = $this->connection->fetchRow($select);
  127. if ($result) {
  128. return $result;
  129. }
  130. }
  131. if ($customer->getEmail()) {
  132. $select = $this->connection
  133. ->select()
  134. ->from($this->getMainTable())
  135. ->where('subscriber_email = ?', $customer->getEmail())
  136. ->where('store_id IN (?)', $storeIds)
  137. ->limit(1);
  138. $result = $this->connection->fetchRow($select);
  139. if ($result) {
  140. return $result;
  141. }
  142. }
  143. return [];
  144. }
  145. /**
  146. * Generates random code for subscription confirmation
  147. *
  148. * @return string
  149. */
  150. protected function _generateRandomCode()
  151. {
  152. return $this->mathRandom->getUniqueHash();
  153. }
  154. /**
  155. * Updates data when subscriber received
  156. *
  157. * @param \Magento\Newsletter\Model\Subscriber $subscriber
  158. * @param \Magento\Newsletter\Model\Queue $queue
  159. * @return $this
  160. * @throws \Magento\Framework\Exception\LocalizedException
  161. */
  162. public function received(\Magento\Newsletter\Model\Subscriber $subscriber, \Magento\Newsletter\Model\Queue $queue)
  163. {
  164. $this->connection->beginTransaction();
  165. try {
  166. $data['letter_sent_at'] = $this->_date->gmtDate();
  167. $this->connection->update(
  168. $this->_subscriberLinkTable,
  169. $data,
  170. ['subscriber_id = ?' => $subscriber->getId(), 'queue_id = ?' => $queue->getId()]
  171. );
  172. $this->connection->commit();
  173. } catch (\Exception $e) {
  174. $this->connection->rollBack();
  175. throw new \Magento\Framework\Exception\LocalizedException(__('We cannot mark as received subscriber.'));
  176. }
  177. return $this;
  178. }
  179. }