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