TransportFactory::createSmtpTransport()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 23
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Nip\Mail\Transport;
5
6
use InvalidArgumentException;
7
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesTransportFactory;
8
use Symfony\Component\Mailer\Transport\AbstractTransport;
9
use Symfony\Component\Mailer\Transport\Dsn;
10
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport as SmtpTransport;
11
use Symfony\Component\Mailer\Transport\TransportInterface;
12
13
/**
14
 * Class TransportManager.
15
 */
16
class TransportFactory
17
{
18
    /**
19
     * @param string $name
20
     *
21
     * @return AbstractTransport
22
     */
23
    public function fromConfig($config)
24
    {
25
        $name = $config['transport'];
26
27
        if ('' === trim($name) || !method_exists($this, $method = 'create' . ucfirst($name) . 'Transport')) {
28
            throw new InvalidArgumentException("Unsupported mail transport [{$name}].");
29
        }
30
31
        return $this->{$method}($config);
32
    }
33
34
    /**
35
     * Create an instance of the Mailgun Swift Transport driver.
36
     */
37
    protected function createSendgridTransport(array $config): SendgridRestTransport
38
    {
39
        $transport = new SendgridRestTransport();
40
        $transport->setApiKey($config['api_key']);
41
42
        return $transport;
43
    }
44
45
    /**
46
     * Create an instance of the Ses Transport driver.
47
     */
48
    protected function createSesTransport(array $config): TransportInterface
49
    {
50
        $dsn = new Dsn(
51
            $config['scheme'] ?? 'ses',
52
            'default',
53
            $config['key'] ?? null,
54
            $config['secret'] ?? null,
55
            null,
56
            [
57
                'region' => $config['region'] ?? null,
58
            ]
59
        );
60
        return (new SesTransportFactory())->create($dsn);
61
    }
62
63
    /**
64
     * Create an instance of the SMTP Swift Transport driver.
65
     *
66
     * @return SmtpTransport
67
     */
68
    protected function createSmtpTransport(array $config)
69
    {// The Swift SMTP transport instance will allow us to use any SMTP backend
70
        // for delivering mail such as Sendgrid, Amazon SES, or a custom server
71
        // a developer has available. We will just pass this configured host.
72
        $transport = new SmtpTransport(
73
            $config['host'],
74
            $config['port']
75
        );
76
77
//        if (!empty($config['encryption'])) {
78
//            $transport->setEncryption($config['encryption']);
79
//        }
80
81
        // Once we have the transport we will check for the presence of a username
82
        // and password. If we have it we will set the credentials on the Swift
83
        // transporter instance so that we'll properly authenticate delivery.
84
        if (isset($config['username'])) {
85
            $transport->setUsername($config['username']);
86
87
            $transport->setPassword($config['password']);
88
        }
89
90
        return $this->configureSmtpTransport($transport, $config);
91
    }
92
93
    /**
94
     * Configure the additional SMTP driver options.
95
     *
96
     * @param SmtpTransport $transport
97
     *
98
     * @return SmtpTransport
99
     */
100
    protected function configureSmtpTransport($transport, array $config)
101
    {
102
        if (isset($config['stream'])) {
103
            $transport->setStreamOptions($config['stream']);
0 ignored issues
show
Bug introduced by
The method setStreamOptions() does not exist on Symfony\Component\Mailer...ort\Smtp\EsmtpTransport. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
            $transport->/** @scrutinizer ignore-call */ 
104
                        setStreamOptions($config['stream']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        }
105
106
        if (isset($config['source_ip'])) {
107
            $transport->setSourceIp($config['source_ip']);
0 ignored issues
show
Bug introduced by
The method setSourceIp() does not exist on Symfony\Component\Mailer...ort\Smtp\EsmtpTransport. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            $transport->/** @scrutinizer ignore-call */ 
108
                        setSourceIp($config['source_ip']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        }
109
110
        if (isset($config['local_domain'])) {
111
            $transport->setLocalDomain($config['local_domain']);
112
        }
113
114
        if (isset($config['timeout'])) {
115
            $transport->setTimeout($config['timeout']);
0 ignored issues
show
Bug introduced by
The method setTimeout() does not exist on Symfony\Component\Mailer...ort\Smtp\EsmtpTransport. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
            $transport->/** @scrutinizer ignore-call */ 
116
                        setTimeout($config['timeout']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
        }
117
118
        if (isset($config['auth_mode'])) {
119
            $transport->setAuthMode($config['auth_mode']);
0 ignored issues
show
Bug introduced by
The method setAuthMode() does not exist on Symfony\Component\Mailer...ort\Smtp\EsmtpTransport. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
            $transport->/** @scrutinizer ignore-call */ 
120
                        setAuthMode($config['auth_mode']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
        }
121
122
        return $transport;
123
    }
124
}
125