Skip to content

Commit 816f403

Browse files
committed
Start working on transport factory
1 parent 9d7e9fc commit 816f403

File tree

14 files changed

+685
-0
lines changed

14 files changed

+685
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Amazon\Factory;
13+
14+
use Symfony\Component\Mailer\Bridge\Amazon;
15+
use Symfony\Component\Mailer\Exception\InvalidDsnException;
16+
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Mailer\Transport\Dsn;
18+
use Symfony\Component\Mailer\Transport\TransportInterface;
19+
20+
/**
21+
* @author Konstantin Myakshin <molodchick@gmail.com>
22+
*/
23+
class SesTransportFactory extends AbstractTransportFactory
24+
{
25+
public function create(Dsn $dsn): TransportInterface
26+
{
27+
$scheme = $dsn->getScheme();
28+
$user = $dsn->getUser();
29+
$pass = $dsn->getPass();
30+
$region = $dsn->getOption('region');
31+
32+
if ('smtp' === $scheme) {
33+
return new Amazon\Smtp\SesTransport($user, $pass, $region, $this->dispatcher, $this->logger);
34+
}
35+
36+
if ('api' === $scheme) {
37+
return new Amazon\Http\Api\SesTransport($user, $pass, $region, $this->client, $this->dispatcher, $this->logger);
38+
}
39+
40+
if ('http' === $scheme) {
41+
return new Amazon\Http\SesTransport($user, $pass, $region, $this->client, $this->dispatcher, $this->logger);
42+
}
43+
44+
throw InvalidDsnException::unsupportedScheme($dsn);
45+
}
46+
47+
public function supports(Dsn $dsn): bool
48+
{
49+
return 'ses' === $dsn->getHost();
50+
}
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Google\Factory;
13+
14+
use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport;
15+
use Symfony\Component\Mailer\Exception\InvalidDsnException;
16+
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Mailer\Transport\Dsn;
18+
use Symfony\Component\Mailer\Transport\TransportInterface;
19+
20+
/**
21+
* @author Konstantin Myakshin <molodchick@gmail.com>
22+
*/
23+
class GmailTransportFactory extends AbstractTransportFactory
24+
{
25+
public function create(Dsn $dsn): TransportInterface
26+
{
27+
if ('smtp' === $dsn->getScheme()) {
28+
return new GmailTransport($dsn->getUser(), $dsn->getPass(), $this->dispatcher, $this->logger);
29+
}
30+
31+
throw InvalidDsnException::unsupportedScheme($dsn);
32+
}
33+
34+
public function supports(Dsn $dsn): bool
35+
{
36+
return 'gmail' === $dsn->getHost();
37+
}
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Factory;
13+
14+
use Symfony\Component\Mailer\Bridge\Mailchimp;
15+
use Symfony\Component\Mailer\Exception\InvalidDsnException;
16+
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Mailer\Transport\Dsn;
18+
use Symfony\Component\Mailer\Transport\TransportInterface;
19+
20+
/**
21+
* @author Konstantin Myakshin <molodchick@gmail.com>
22+
*/
23+
class MandrillTransportFactory extends AbstractTransportFactory
24+
{
25+
public function create(Dsn $dsn): TransportInterface
26+
{
27+
$scheme = $dsn->getScheme();
28+
$user = $dsn->getUser();
29+
30+
if ('smtp' === $scheme) {
31+
return new Mailchimp\Smtp\MandrillTransport($user, $dsn->getPass(), $this->dispatcher, $this->logger);
32+
}
33+
34+
if ('api' === $scheme) {
35+
return new Mailchimp\Http\Api\MandrillTransport($user, $this->client, $this->dispatcher, $this->logger);
36+
}
37+
38+
if ('http' === $scheme) {
39+
return new Mailchimp\Http\MandrillTransport($user, $this->client, $this->dispatcher, $this->logger);
40+
}
41+
42+
throw InvalidDsnException::unsupportedScheme($dsn);
43+
}
44+
45+
public function supports(Dsn $dsn): bool
46+
{
47+
return 'mandrill' === $dsn->getHost();
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Mailgun\Factory;
13+
14+
use Symfony\Component\Mailer\Bridge\Mailgun;
15+
use Symfony\Component\Mailer\Exception\InvalidDsnException;
16+
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Mailer\Transport\Dsn;
18+
use Symfony\Component\Mailer\Transport\TransportInterface;
19+
20+
/**
21+
* @author Konstantin Myakshin <molodchick@gmail.com>
22+
*/
23+
class MailgunTransportFactory extends AbstractTransportFactory
24+
{
25+
public function create(Dsn $dsn): TransportInterface
26+
{
27+
$scheme = $dsn->getScheme();
28+
$user = $dsn->getUser();
29+
$pass = $dsn->getPass();
30+
31+
if ('smtp' === $scheme) {
32+
return new Mailgun\Smtp\MailgunTransport($user, $pass, $this->dispatcher, $this->logger);
33+
}
34+
35+
if ('http' === $scheme) {
36+
return new Mailgun\Http\MailgunTransport($user, $pass, $this->client, $this->dispatcher, $this->logger);
37+
}
38+
39+
if ('api' === $scheme) {
40+
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $this->client, $this->dispatcher, $this->logger);
41+
}
42+
43+
throw InvalidDsnException::unsupportedScheme($dsn);
44+
}
45+
46+
public function supports(Dsn $dsn): bool
47+
{
48+
return 'mailgun' === $dsn->getHost();
49+
}
50+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Postmark\Factory;
13+
14+
use Symfony\Component\Mailer\Bridge\Postmark;
15+
use Symfony\Component\Mailer\Exception\InvalidDsnException;
16+
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Mailer\Transport\Dsn;
18+
use Symfony\Component\Mailer\Transport\TransportInterface;
19+
20+
/**
21+
* @author Konstantin Myakshin <molodchick@gmail.com>
22+
*/
23+
class PostmarkTransportFactory extends AbstractTransportFactory
24+
{
25+
public function create(Dsn $dsn): TransportInterface
26+
{
27+
$scheme = $dsn->getScheme();
28+
$user = $dsn->getUser();
29+
30+
if ('smtp' === $scheme) {
31+
return new Postmark\Smtp\PostmarkTransport($user, $this->dispatcher, $this->logger);
32+
}
33+
34+
if ('api' === $scheme) {
35+
return new Postmark\Http\Api\PostmarkTransport($user, $this->client, $this->dispatcher, $this->logger);
36+
}
37+
38+
throw InvalidDsnException::unsupportedScheme($dsn);
39+
}
40+
41+
public function supports(Dsn $dsn): bool
42+
{
43+
return 'postmark' === $dsn->getHost();
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Factory;
13+
14+
use Symfony\Component\Mailer\Bridge\Sendgrid;
15+
use Symfony\Component\Mailer\Exception\InvalidDsnException;
16+
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Mailer\Transport\Dsn;
18+
use Symfony\Component\Mailer\Transport\TransportInterface;
19+
20+
/**
21+
* @author Konstantin Myakshin <molodchick@gmail.com>
22+
*/
23+
class SendgridTransportFactory extends AbstractTransportFactory
24+
{
25+
public function create(Dsn $dsn): TransportInterface
26+
{
27+
if ('smtp' === $dsn->getScheme()) {
28+
return new Sendgrid\Smtp\SendgridTransport($dsn->getUser(), $this->dispatcher, $this->logger);
29+
}
30+
31+
if ('api' === $dsn->getScheme()) {
32+
return new Sendgrid\Http\Api\SendgridTransport($dsn->getUser(), $this->client, $this->dispatcher, $this->logger);
33+
}
34+
35+
throw InvalidDsnException::unsupportedScheme($dsn);
36+
}
37+
38+
public function supports(Dsn $dsn): bool
39+
{
40+
return 'sendgrid' === $dsn->getHost();
41+
}
42+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Exception;
13+
14+
use Symfony\Component\Mailer\Bridge;
15+
use Symfony\Component\Mailer\Transport\Dsn;
16+
17+
/**
18+
* @author Konstantin Myakshin <molodchick@gmail.com>
19+
*/
20+
class InvalidDsnException extends LogicException
21+
{
22+
public static function unsupportedScheme(Dsn $dsn): self
23+
{
24+
return new self(sprintf('The "%s" scheme is not supported for mailer "%s".', $dsn->getScheme(), $dsn->getHost()));
25+
}
26+
27+
public static function unsupportedHost(Dsn $dsn): self
28+
{
29+
$hostsToPackages = [
30+
'gmail' => [
31+
'class' => Bridge\Google\Factory\GmailTransportFactory::class,
32+
'package' => 'symfony/google-mailer',
33+
],
34+
'mailgun' => [
35+
'class' => Bridge\Mailgun\Factory\MailgunTransportFactory::class,
36+
'package' => 'symfony/mailgun-mailer',
37+
],
38+
'postmark' => [
39+
'class' => Bridge\Postmark\Factory\PostmarkTransportFactory::class,
40+
'package' => 'symfony/postmark-mailer',
41+
],
42+
'sendgrid' => [
43+
'class' => Bridge\Sendgrid\Factory\SendgridTransportFactory::class,
44+
'package' => 'symfony/sendgrid-mailer',
45+
],
46+
'ses' => [
47+
'class' => Bridge\Amazon\Factory\SesTransportFactory::class,
48+
'package' => 'symfony/amazon-mailer',
49+
],
50+
'mandrill' => [
51+
'class' => Bridge\Mailchimp\Factory\MandrillTransportFactory::class,
52+
'package' => 'symfony/mailchimp-mailer',
53+
],
54+
];
55+
56+
$host = $dsn->getHost();
57+
$package = $hostsToPackages[$host] ?? null;
58+
if ($package && !class_exists($package['class'])) {
59+
return new self(sprintf('Unable to send emails via "%s" as the bridge is not installed. Try running "composer require %s".', $host, $package['package']));
60+
}
61+
62+
return new self(sprintf('The "%s" mailer is not supported.', $host));
63+
}
64+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Transport;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
18+
/**
19+
* @author Konstantin Myakshin <molodchick@gmail.com>
20+
*/
21+
abstract class AbstractTransportFactory implements TransportFactoryInterface
22+
{
23+
protected $client;
24+
protected $dispatcher;
25+
protected $logger;
26+
27+
public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
28+
{
29+
$this->client = $client;
30+
$this->dispatcher = $dispatcher;
31+
$this->logger = $logger;
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy