Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Queue.php
Go to the documentation of this file.
1 <?php
7 
10 
41 {
47  protected $_template;
48 
55 
61  protected $_saveStoresFlag = false;
62 
68  protected $_stores = [];
69 
70  const STATUS_NEVER = 0;
71 
72  const STATUS_SENDING = 1;
73 
74  const STATUS_CANCEL = 2;
75 
76  const STATUS_SENT = 3;
77 
78  const STATUS_PAUSE = 4;
79 
85  protected $_templateFilter;
86 
92  protected $_date;
93 
99  protected $_problemFactory;
100 
106  protected $_templateFactory;
107 
112 
118  private $timezone;
119 
135  public function __construct(
136  \Magento\Framework\Model\Context $context,
137  \Magento\Framework\Registry $registry,
138  \Magento\Newsletter\Model\Template\Filter $templateFilter,
139  \Magento\Framework\Stdlib\DateTime\DateTime $date,
140  \Magento\Newsletter\Model\TemplateFactory $templateFactory,
141  \Magento\Newsletter\Model\ProblemFactory $problemFactory,
142  \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory,
143  \Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder,
144  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
145  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
146  array $data = [],
147  TimezoneInterface $timezone = null
148  ) {
149  parent::__construct(
150  $context,
151  $registry,
152  $resource,
153  $resourceCollection,
154  $data
155  );
156  $this->_templateFilter = $templateFilter;
157  $this->_date = $date;
158  $this->_templateFactory = $templateFactory;
159  $this->_problemFactory = $problemFactory;
160  $this->_subscribersCollection = $subscriberCollectionFactory->create();
161  $this->_transportBuilder = $transportBuilder;
162  $this->timezone = $timezone ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
163  TimezoneInterface::class
164  );
165  }
166 
172  protected function _construct()
173  {
174  parent::_construct();
175  $this->_init(\Magento\Newsletter\Model\ResourceModel\Queue::class);
176  }
177 
183  public function isNew()
184  {
185  return $this->getQueueStatus() === null;
186  }
187 
194  public function setQueueStartAtByString($startAt)
195  {
196  if ($startAt === null || $startAt == '') {
197  $this->setQueueStartAt(null);
198  } else {
199  $this->setQueueStartAt($this->timezone->convertConfigTimeToUtc($startAt));
200  }
201  return $this;
202  }
203 
211  public function sendPerSubscriber($count = 20)
212  {
213  if ($this->getQueueStatus() != self::STATUS_SENDING &&
214  ($this->getQueueStatus() != self::STATUS_NEVER &&
215  $this->getQueueStartAt())
216  ) {
217  return $this;
218  }
219 
220  if (!$this->_subscribersCollection->getQueueJoinedFlag()) {
221  $this->_subscribersCollection->useQueue($this);
222  }
223 
224  if ($this->_subscribersCollection->getSize() == 0) {
225  $this->_finishQueue();
226  return $this;
227  }
228 
229  $collection = $this->_subscribersCollection->useOnlyUnsent()->showCustomerInfo()->setPageSize(
230  $count
231  )->setCurPage(
232  1
233  )->load();
234 
235  $this->_transportBuilder->setTemplateData(
236  [
237  'template_subject' => $this->getNewsletterSubject(),
238  'template_text' => $this->getNewsletterText(),
239  'template_styles' => $this->getNewsletterStyles(),
240  'template_filter' => $this->_templateFilter,
241  'template_type' => self::TYPE_HTML,
242  ]
243  );
244 
246  foreach ($collection->getItems() as $item) {
247  $transport = $this->_transportBuilder->setTemplateOptions(
248  ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $item->getStoreId()]
249  )->setTemplateVars(
250  ['subscriber' => $item]
251  )->setFrom(
252  ['name' => $this->getNewsletterSenderName(), 'email' => $this->getNewsletterSenderEmail()]
253  )->addTo(
254  $item->getSubscriberEmail(),
255  $item->getSubscriberFullName()
256  )->getTransport();
257 
258  try {
259  $transport->sendMessage();
260  } catch (\Magento\Framework\Exception\MailException $e) {
262  $problem = $this->_problemFactory->create();
263  $problem->addSubscriberData($item);
264  $problem->addQueueData($this);
265  $problem->addErrorData($e);
266  $problem->save();
267  }
268  $item->received($this);
269  }
270 
271  if (count($collection->getItems()) < $count - 1 || count($collection->getItems()) == 0) {
272  $this->_finishQueue();
273  }
274  return $this;
275  }
276 
282  protected function _finishQueue()
283  {
284  $this->setQueueFinishAt($this->_date->gmtDate());
285  $this->setQueueStatus(self::STATUS_SENT);
286  $this->save();
287 
288  return $this;
289  }
290 
296  public function getDataForSave()
297  {
298  $data = [];
299  $data['template_id'] = $this->getTemplateId();
300  $data['queue_status'] = $this->getQueueStatus();
301  $data['queue_start_at'] = $this->getQueueStartAt();
302  $data['queue_finish_at'] = $this->getQueueFinishAt();
303  return $data;
304  }
305 
312  public function addSubscribersToQueue(array $subscriberIds)
313  {
314  $this->_getResource()->addSubscribersToQueue($this, $subscriberIds);
315  return $this;
316  }
317 
324  public function setSaveStoresFlag($value)
325  {
326  $this->_saveStoresFlag = (bool)$value;
327  return $this;
328  }
329 
336  public function getSaveStoresFlag()
337  {
338  return $this->_saveStoresFlag;
339  }
340 
347  public function setStores(array $storesIds)
348  {
349  $this->setSaveStoresFlag(true);
350  $this->_stores = $storesIds;
351  return $this;
352  }
353 
359  public function getStores()
360  {
361  if (!$this->_stores) {
362  $this->_stores = $this->_getResource()->getStores($this);
363  }
364 
365  return $this->_stores;
366  }
367 
373  public function getTemplate()
374  {
375  if ($this->_template === null) {
376  $this->_template = $this->_templateFactory->create()->load($this->getTemplateId());
377  }
378  return $this->_template;
379  }
380 
386  public function isPlain()
387  {
388  return $this->getType() == self::TYPE_TEXT;
389  }
390 
396  public function getType()
397  {
398  return $this->getNewsletterType();
399  }
400 }
setQueueStartAtByString($startAt)
Definition: Queue.php:194
$count
Definition: recent.phtml:13
addSubscribersToQueue(array $subscriberIds)
Definition: Queue.php:312
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
$problem
Definition: problems.php:9
setStores(array $storesIds)
Definition: Queue.php:347
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Newsletter\Model\Template\Filter $templateFilter, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Newsletter\Model\TemplateFactory $templateFactory, \Magento\Newsletter\Model\ProblemFactory $problemFactory, \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory, \Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], TimezoneInterface $timezone=null)
Definition: Queue.php:135