MessagePlugin.php 3.12 KB
Newer Older
Ketan's avatar
Ketan committed
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
<?php

namespace Dotdigitalgroup\Email\Plugin;

use Dotdigitalgroup\Email\Helper\Transactional;
use Magento\Email\Model\ResourceModel\Template;
use Magento\Email\Model\TemplateFactory;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Registry;

class MessagePlugin
{
    /**
     * @var Transactional
     */
    private $transactionalHelper;

    /**
     * @var Template
     */
    private $templateResource;

    /**
     * @var TemplateFactory
     */
    private $templateFactory;

    /**
     * @var Registry
     */
    private $registry;

    /**
     * MessagePlugin constructor.
     * @param Registry $registry
     * @param Transactional $transactionalHelper
     * @param Template $templateResource
     * @param TemplateFactory $template
     */
    public function __construct(
        Registry $registry,
        Transactional $transactionalHelper,
        Template $templateResource,
        TemplateFactory $template
    ) {
        $this->registry = $registry;
        $this->templateFactory = $template;
        $this->templateResource = $templateResource;
        $this->transactionalHelper = $transactionalHelper;
    }

    /**
     * @param MessageInterface $message
     * @param string $body
     *
     * @return mixed
     */
    public function afterSetBody(MessageInterface $message, $body)
    {
        $templateId = $this->isTemplate();
        if ($templateId && $this->shouldIntercept()) {
            $template = $this->loadTemplate($templateId);
            if ($this->isDotmailerTemplateCode($template->getTemplateCode())) {
                $this->handleZendMailMessage($message);
                $this->setMessageFromAddressFromTemplate($message, $template);
            }
        }
        return $body;
    }

    /**
     * @return int
     */
    private function isTemplate()
    {
        return $this->registry->registry('dotmailer_current_template_id');
    }

    /**
     * @return bool
     */
    private function shouldIntercept()
    {
        $storeId = $this->registry->registry('transportBuilderPluginStoreId');
        return $this->transactionalHelper->isEnabled($storeId);
    }

    /**
     * @param $templateId
     *
     * @return \Magento\Email\Model\Template
     */
    private function loadTemplate($templateId)
    {
        $template = $this->templateFactory->create();
        $this->templateResource->load($template, $templateId);

        return $template;
    }

    /**
     * @param $templateCode
     *
     * @return bool
     */
    private function isDotmailerTemplateCode($templateCode)
    {
        return $this->transactionalHelper->isDotmailerTemplate($templateCode);
    }

    /**
     * @param MessageInterface $message
     */
    private function handleZendMailMessage($message)
    {
        if ($message instanceof \Zend_Mail) {
            $message->clearFrom();
        }
    }

    /**
     * @param MessageInterface $message
     * @param \Magento\Email\Model\Template $template
     */
    private function setMessageFromAddressFromTemplate($message, $template)
    {
        $message->setFrom($template->getTemplateSenderEmail(), $template->getTemplateSenderName());
    }
}