Passed
Push — master ( 03a8f7...7b2ab5 )
by Gabriel
12:52
created

TransportFactory::configureSmtpTransport()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

82
            $transport->/** @scrutinizer ignore-call */ 
83
                        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...
83
        }
84
85
        if (isset($config['source_ip'])) {
86
            $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

86
            $transport->/** @scrutinizer ignore-call */ 
87
                        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...
87
        }
88
89
        if (isset($config['local_domain'])) {
90
            $transport->setLocalDomain($config['local_domain']);
91
        }
92
93
        if (isset($config['timeout'])) {
94
            $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

94
            $transport->/** @scrutinizer ignore-call */ 
95
                        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...
95
        }
96
97
        if (isset($config['auth_mode'])) {
98
            $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

98
            $transport->/** @scrutinizer ignore-call */ 
99
                        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...
99
        }
100
101
        return $transport;
102
    }
103
}
104