You are here

class TransportFactory in Swift Mailer 8

Same name and namespace in other branches
  1. 8.2 src/TransportFactory.php \Drupal\swiftmailer\TransportFactory

A service that instantiates transport subsystems.

Hierarchy

Expanded class hierarchy of TransportFactory

1 string reference to 'TransportFactory'
swiftmailer.services.yml in ./swiftmailer.services.yml
swiftmailer.services.yml
1 service uses TransportFactory
swiftmailer.transport in ./swiftmailer.services.yml
Drupal\swiftmailer\TransportFactory

File

src/TransportFactory.php, line 15

Namespace

Drupal\swiftmailer
View source
class TransportFactory implements TransportFactoryInterface {

  /**
   * The transport configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $transportConfig;

  /**
   * Constructs a TransportFactory object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->transportConfig = $config_factory
      ->get('swiftmailer.transport');
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultTransportMethod() {
    return $this->transportConfig
      ->get('transport');
  }

  /**
   * {@inheritdoc}
   */
  public function getTransport($transport_type) {

    // Configure the mailer based on the configured transport type.
    switch ($transport_type) {
      case SWIFTMAILER_TRANSPORT_SMTP:

        // Get transport configuration.
        $host = $this->transportConfig
          ->get('smtp_host');
        $port = $this->transportConfig
          ->get('smtp_port');
        $encryption = $this->transportConfig
          ->get('smtp_encryption');
        $provider = $this->transportConfig
          ->get('smtp_credential_provider');
        $username = NULL;
        $password = NULL;
        if ($provider === 'swiftmailer') {
          $username = $this->transportConfig
            ->get('smtp_credentials')['swiftmailer']['username'];
          $password = $this->transportConfig
            ->get('smtp_credentials')['swiftmailer']['password'];
        }
        elseif ($provider === 'key') {

          /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
          $storage = \Drupal::entityTypeManager()
            ->getStorage('key');

          /** @var \Drupal\key\KeyInterface $username_key */
          $username_key = $storage
            ->load($this->transportConfig
            ->get('smtp_credentials')['key']['username']);
          if ($username_key) {
            $username = $username_key
              ->getKeyValue();
          }

          /** @var \Drupal\key\KeyInterface $password_key */
          $password_key = $storage
            ->load($this->transportConfig
            ->get('smtp_credentials')['key']['password']);
          if ($password_key) {
            $password = $password_key
              ->getKeyValue();
          }
        }
        elseif ($provider === 'multikey') {

          /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
          $storage = \Drupal::entityTypeManager()
            ->getStorage('key');

          /** @var \Drupal\key\KeyInterface $username_key */
          $user_password_key = $storage
            ->load($this->transportConfig
            ->get('smtp_credentials')['multikey']['user_password']);
          if ($user_password_key) {
            $values = $user_password_key
              ->getKeyValues();
            $username = $values['username'];
            $password = $values['password'];
          }
        }

        // Instantiate transport.
        $transport = \Swift_SmtpTransport::newInstance($host, $port);
        $transport
          ->setLocalDomain('[127.0.0.1]');

        // Set encryption (if any).
        if (!empty($encryption)) {
          $transport
            ->setEncryption($encryption);
        }

        // Set username (if any).
        if (!empty($username)) {
          $transport
            ->setUsername($username);
        }

        // Set password (if any).
        if (!empty($password)) {
          $transport
            ->setPassword($password);
        }
        break;
      case SWIFTMAILER_TRANSPORT_SENDMAIL:

        // Get transport configuration.
        $path = $this->transportConfig
          ->get('sendmail_path');
        $mode = $this->transportConfig
          ->get('sendmail_mode');

        // Instantiate transport.
        $transport = Swift_SendmailTransport::newInstance($path . ' -' . $mode);
        break;
      case SWIFTMAILER_TRANSPORT_NATIVE:

        // Instantiate transport.
        $transport = Swift_MailTransport::newInstance();
        break;
      case SWIFTMAILER_TRANSPORT_SPOOL:

        // Instantiate transport.
        $spooldir = $this->transportConfig
          ->get('spool_directory');
        $spool = new Swift_FileSpool($spooldir);
        $transport = Swift_SpoolTransport::newInstance($spool);
        break;
      case SWIFTMAILER_TRANSPORT_NULL:
        $transport = Swift_NullTransport::newInstance();
        break;
    }
    if (!isset($transport)) {
      throw new \LogicException('The transport method is undefined.');
    }
    return $transport;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TransportFactory::$transportConfig protected property The transport configuration.
TransportFactory::getDefaultTransportMethod public function Returns the transport type configured as default. Overrides TransportFactoryInterface::getDefaultTransportMethod
TransportFactory::getTransport public function Instantiates a transport object of the specified type. Overrides TransportFactoryInterface::getTransport
TransportFactory::__construct public function Constructs a TransportFactory object.