<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_EmailAttachments
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

declare(strict_types=1);

namespace Mageplaza\EmailAttachments\Mail;

use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Mail\EmailMessageInterface;
use Magento\Framework\Mail\MailMessageInterface;
use Mageplaza\EmailAttachments\Helper\Data;
use Psr\Log\LoggerInterface;
use Laminas\Mail\Message;
use Laminas\Mail\Transport\Sendmail;

/**
 * Class that responsible for filling some message data before transporting it.
 * @see Lamias\Mail\Transport\Sendmail is used for transport
 */
class Transport extends \Magento\Email\Model\Transport
{
    /**
     * Whether return path should be set or no.
     *
     * Possible values are:
     * 0 - no
     * 1 - yes (set value as FROM address)
     * 2 - use custom value
     *
     * @var int
     */
    private $isSetReturnPath;

    /**
     * @var string|null
     */
    private $returnPathValue;

    /**
     * @var MailMessageInterface
     */
    private $message;

    /**
     * @var Data
     */
    private $dataHelper;

    /**
     * @param Data $dataHelper
     * @param EmailMessageInterface $message
     * @param ScopeConfigInterface $scopeConfig
     * @param LoggerInterface|null $logger
     */
    public function __construct(
        Data $dataHelper,
        EmailMessageInterface $message,
        ScopeConfigInterface $scopeConfig,
        ?LoggerInterface $logger = null
    )
    {
        $this->dataHelper = $dataHelper;

        parent::__construct($message, $scopeConfig, $logger);
    }

    /**
     * @inheritdoc
     */
    public function sendMessage(): void
    {
        if (!$this->dataHelper->isAbove248()) {
            try {
                $zendMessage = Message::fromString($this->getMessage()->getRawMessage())->setEncoding('utf-8');
                if ($this->isSetReturnPath === 2 && $this->returnPathValue) {
                    $zendMessage->setSender($this->returnPathValue);
                } elseif ($this->isSetReturnPath === 1 && $zendMessage->getFrom()->count()) {
                    $fromAddressList = $zendMessage->getFrom();
                    $fromAddressList->rewind();
                    $zendMessage->setSender($fromAddressList->current()->getEmail());
                }

                $zendTransport = $this->dataHelper->createObject(\Laminas\Mail\Transport\Sendmail::class);
                $zendTransport->send($zendMessage);
            } catch (Exception $e) {
                throw new MailException(__($e->getMessage()), $e);
            }
        } else {
            parent::sendMessage();
        }
    }

    /**
     * @inheritdoc
     */
    public function getMessage(): EmailMessageInterface
    {
        return parent::getMessage();
    }
}
