Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Subscriber.php
Go to the documentation of this file.
1 <?php
7 
17 {
23  protected $connection;
24 
31 
37  protected $_messagesScope = 'newsletter/session';
38 
44  protected $_date;
45 
49  protected $mathRandom;
50 
59  public function __construct(
60  \Magento\Framework\Model\ResourceModel\Db\Context $context,
61  \Magento\Framework\Stdlib\DateTime\DateTime $date,
62  \Magento\Framework\Math\Random $mathRandom,
63  $connectionName = null
64  ) {
65  $this->_date = $date;
66  $this->mathRandom = $mathRandom;
67  parent::__construct($context, $connectionName);
68  }
69 
75  protected function _construct()
76  {
77  $this->_init('newsletter_subscriber', 'subscriber_id');
78  $this->_subscriberLinkTable = $this->getTable('newsletter_queue_link');
79  $this->connection = $this->getConnection();
80  }
81 
88  public function setMessagesScope($scope)
89  {
90  $this->_messagesScope = $scope;
91  }
92 
99  public function loadByEmail($subscriberEmail)
100  {
101  $select = $this->connection->select()->from($this->getMainTable())->where('subscriber_email=:subscriber_email');
102 
103  $result = $this->connection->fetchRow($select, ['subscriber_email' => $subscriberEmail]);
104 
105  if (!$result) {
106  return [];
107  }
108 
109  return $result;
110  }
111 
118  public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
119  {
120  $select = $this->connection
121  ->select()
122  ->from($this->getMainTable())
123  ->where('customer_id=:customer_id and store_id=:store_id');
124 
125  $result = $this->connection
126  ->fetchRow(
127  $select,
128  [
129  'customer_id' => $customer->getId(),
130  'store_id' => $customer->getStoreId()
131  ]
132  );
133 
134  if ($result) {
135  return $result;
136  }
137 
138  $select = $this->connection
139  ->select()
140  ->from($this->getMainTable())
141  ->where('subscriber_email=:subscriber_email and store_id=:store_id');
142 
143  $result = $this->connection
144  ->fetchRow(
145  $select,
146  [
147  'subscriber_email' => $customer->getEmail(),
148  'store_id' => $customer->getStoreId()
149  ]
150  );
151 
152  if ($result) {
153  return $result;
154  }
155 
156  return [];
157  }
158 
164  protected function _generateRandomCode()
165  {
166  return $this->mathRandom->getUniqueHash();
167  }
168 
177  public function received(\Magento\Newsletter\Model\Subscriber $subscriber, \Magento\Newsletter\Model\Queue $queue)
178  {
179  $this->connection->beginTransaction();
180  try {
181  $data['letter_sent_at'] = $this->_date->gmtDate();
182  $this->connection->update(
183  $this->_subscriberLinkTable,
184  $data,
185  ['subscriber_id = ?' => $subscriber->getId(), 'queue_id = ?' => $queue->getId()]
186  );
187  $this->connection->commit();
188  } catch (\Exception $e) {
189  $this->connection->rollBack();
190  throw new \Magento\Framework\Exception\LocalizedException(__('We cannot mark as received subscriber.'));
191  }
192  return $this;
193  }
194 }
$queue
Definition: queue.php:21
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Framework\Math\Random $mathRandom, $connectionName=null)
Definition: Subscriber.php:59
$customer
Definition: customers.php:11
__()
Definition: __.php:13
loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
Definition: Subscriber.php:118
received(\Magento\Newsletter\Model\Subscriber $subscriber, \Magento\Newsletter\Model\Queue $queue)
Definition: Subscriber.php:177
$subscriber
Definition: subscribers.php:20