Skip to content

Commit

Permalink
Start working on transport factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed Jun 11, 2019
1 parent 9d7e9fc commit 816f403
Show file tree
Hide file tree
Showing 14 changed files with 685 additions and 0 deletions.
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Amazon\Factory;

use Symfony\Component\Mailer\Bridge\Amazon;
use Symfony\Component\Mailer\Exception\InvalidDsnException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
class SesTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$user = $dsn->getUser();
$pass = $dsn->getPass();
$region = $dsn->getOption('region');

if ('smtp' === $scheme) {
return new Amazon\Smtp\SesTransport($user, $pass, $region, $this->dispatcher, $this->logger);
}

if ('api' === $scheme) {
return new Amazon\Http\Api\SesTransport($user, $pass, $region, $this->client, $this->dispatcher, $this->logger);
}

if ('http' === $scheme) {
return new Amazon\Http\SesTransport($user, $pass, $region, $this->client, $this->dispatcher, $this->logger);
}

throw InvalidDsnException::unsupportedScheme($dsn);
}

public function supports(Dsn $dsn): bool
{
return 'ses' === $dsn->getHost();
}
}
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Google\Factory;

use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport;
use Symfony\Component\Mailer\Exception\InvalidDsnException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
class GmailTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
if ('smtp' === $dsn->getScheme()) {
return new GmailTransport($dsn->getUser(), $dsn->getPass(), $this->dispatcher, $this->logger);
}

throw InvalidDsnException::unsupportedScheme($dsn);
}

public function supports(Dsn $dsn): bool
{
return 'gmail' === $dsn->getHost();
}
}
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Mailchimp\Factory;

use Symfony\Component\Mailer\Bridge\Mailchimp;
use Symfony\Component\Mailer\Exception\InvalidDsnException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
class MandrillTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$user = $dsn->getUser();

if ('smtp' === $scheme) {
return new Mailchimp\Smtp\MandrillTransport($user, $dsn->getPass(), $this->dispatcher, $this->logger);
}

if ('api' === $scheme) {
return new Mailchimp\Http\Api\MandrillTransport($user, $this->client, $this->dispatcher, $this->logger);
}

if ('http' === $scheme) {
return new Mailchimp\Http\MandrillTransport($user, $this->client, $this->dispatcher, $this->logger);
}

throw InvalidDsnException::unsupportedScheme($dsn);
}

public function supports(Dsn $dsn): bool
{
return 'mandrill' === $dsn->getHost();
}
}
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Mailgun\Factory;

use Symfony\Component\Mailer\Bridge\Mailgun;
use Symfony\Component\Mailer\Exception\InvalidDsnException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
class MailgunTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$user = $dsn->getUser();
$pass = $dsn->getPass();

if ('smtp' === $scheme) {
return new Mailgun\Smtp\MailgunTransport($user, $pass, $this->dispatcher, $this->logger);
}

if ('http' === $scheme) {
return new Mailgun\Http\MailgunTransport($user, $pass, $this->client, $this->dispatcher, $this->logger);
}

if ('api' === $scheme) {
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $this->client, $this->dispatcher, $this->logger);
}

throw InvalidDsnException::unsupportedScheme($dsn);
}

public function supports(Dsn $dsn): bool
{
return 'mailgun' === $dsn->getHost();
}
}
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Postmark\Factory;

use Symfony\Component\Mailer\Bridge\Postmark;
use Symfony\Component\Mailer\Exception\InvalidDsnException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
class PostmarkTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$user = $dsn->getUser();

if ('smtp' === $scheme) {
return new Postmark\Smtp\PostmarkTransport($user, $this->dispatcher, $this->logger);
}

if ('api' === $scheme) {
return new Postmark\Http\Api\PostmarkTransport($user, $this->client, $this->dispatcher, $this->logger);
}

throw InvalidDsnException::unsupportedScheme($dsn);
}

public function supports(Dsn $dsn): bool
{
return 'postmark' === $dsn->getHost();
}
}
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Sendgrid\Factory;

use Symfony\Component\Mailer\Bridge\Sendgrid;
use Symfony\Component\Mailer\Exception\InvalidDsnException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
class SendgridTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
if ('smtp' === $dsn->getScheme()) {
return new Sendgrid\Smtp\SendgridTransport($dsn->getUser(), $this->dispatcher, $this->logger);
}

if ('api' === $dsn->getScheme()) {
return new Sendgrid\Http\Api\SendgridTransport($dsn->getUser(), $this->client, $this->dispatcher, $this->logger);
}

throw InvalidDsnException::unsupportedScheme($dsn);
}

public function supports(Dsn $dsn): bool
{
return 'sendgrid' === $dsn->getHost();
}
}
64 changes: 64 additions & 0 deletions src/Symfony/Component/Mailer/Exception/InvalidDsnException.php
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Exception;

use Symfony\Component\Mailer\Bridge;
use Symfony\Component\Mailer\Transport\Dsn;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
class InvalidDsnException extends LogicException
{
public static function unsupportedScheme(Dsn $dsn): self
{
return new self(sprintf('The "%s" scheme is not supported for mailer "%s".', $dsn->getScheme(), $dsn->getHost()));
}

public static function unsupportedHost(Dsn $dsn): self
{
$hostsToPackages = [
'gmail' => [
'class' => Bridge\Google\Factory\GmailTransportFactory::class,
'package' => 'symfony/google-mailer',
],
'mailgun' => [
'class' => Bridge\Mailgun\Factory\MailgunTransportFactory::class,
'package' => 'symfony/mailgun-mailer',
],
'postmark' => [
'class' => Bridge\Postmark\Factory\PostmarkTransportFactory::class,
'package' => 'symfony/postmark-mailer',
],
'sendgrid' => [
'class' => Bridge\Sendgrid\Factory\SendgridTransportFactory::class,
'package' => 'symfony/sendgrid-mailer',
],
'ses' => [
'class' => Bridge\Amazon\Factory\SesTransportFactory::class,
'package' => 'symfony/amazon-mailer',
],
'mandrill' => [
'class' => Bridge\Mailchimp\Factory\MandrillTransportFactory::class,
'package' => 'symfony/mailchimp-mailer',
],
];

$host = $dsn->getHost();
$package = $hostsToPackages[$host] ?? null;
if ($package && !class_exists($package['class'])) {
return new self(sprintf('Unable to send emails via "%s" as the bridge is not installed. Try running "composer require %s".', $host, $package['package']));
}

return new self(sprintf('The "%s" mailer is not supported.', $host));
}
}
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Transport;

use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
abstract class AbstractTransportFactory implements TransportFactoryInterface
{
protected $client;
protected $dispatcher;
protected $logger;

public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->client = $client;
$this->dispatcher = $dispatcher;
$this->logger = $logger;
}
}

0 comments on commit 816f403

Please sign in to comment.