Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EmailSenderHandler.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Sales\Model;
7 
9 
17 {
23  protected $emailSender;
24 
30  protected $entityResource;
31 
37  protected $entityCollection;
38 
44  protected $globalConfig;
45 
49  private $identityContainer;
50 
54  private $storeManager;
55 
65  public function __construct(
66  \Magento\Sales\Model\Order\Email\Sender $emailSender,
67  \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource,
68  \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection,
70  IdentityInterface $identityContainer = null,
71  \Magento\Store\Model\StoreManagerInterface $storeManager = null
72  ) {
73  $this->emailSender = $emailSender;
74  $this->entityResource = $entityResource;
75  $this->entityCollection = $entityCollection;
76  $this->globalConfig = $globalConfig;
77 
78  $this->identityContainer = $identityContainer ?: \Magento\Framework\App\ObjectManager::getInstance()
79  ->get(\Magento\Sales\Model\Order\Email\Container\NullIdentity::class);
80  $this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()
81  ->get(\Magento\Store\Model\StoreManagerInterface::class);
82  }
83 
88  public function sendEmails()
89  {
90  if ($this->globalConfig->getValue('sales_email/general/async_sending')) {
91  $this->entityCollection->addFieldToFilter('send_email', ['eq' => 1]);
92  $this->entityCollection->addFieldToFilter('email_sent', ['null' => true]);
93  $this->entityCollection->setPageSize(
94  $this->globalConfig->getValue('sales_email/general/sending_limit')
95  );
96 
98  $stores = $this->getStores(clone $this->entityCollection);
99 
101  foreach ($stores as $store) {
102  $this->identityContainer->setStore($store);
103  if (!$this->identityContainer->isEnabled()) {
104  continue;
105  }
107  $entityCollection->addFieldToFilter('store_id', $store->getId());
108 
110  foreach ($entityCollection->getItems() as $item) {
111  if ($this->emailSender->send($item, true)) {
112  $this->entityResource->save(
113  $item->setEmailSent(true)
114  );
115  }
116  }
117  }
118  }
119  }
120 
128  private function getStores(
129  \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection
130  ): array {
131  $stores = [];
132 
133  $entityCollection->addAttributeToSelect('store_id')->getSelect()->group('store_id');
135  foreach ($entityCollection->getItems() as $item) {
137  $store = $this->storeManager->getStore($item->getStoreId());
138  $stores[$item->getStoreId()] = $store;
139  }
140 
141  return $stores;
142  }
143 }
__construct(\Magento\Sales\Model\Order\Email\Sender $emailSender, \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource, \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection, \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig, IdentityInterface $identityContainer=null, \Magento\Store\Model\StoreManagerInterface $storeManager=null)