SmtpTransport::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Da\Mailer\Transport;
4
5
use Symfony\Component\Mailer\Transport\Dsn;
6
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
7
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
8
9
class SmtpTransport implements TransportInterface
10
{
11
    /**
12
     * @var EsmtpTransport
13
     */
14
    private $instance;
15
/**
16
     * @var string the mail server host name or ip
17
     */
18
    private $host;
19
/**
20
     * @var int the mail server port
21
     */
22
    private $port;
23
/**
24
     * @var array the extra options for the Smtp transport -ie username, password, encryption, authMode
25
     */
26
    private $options = [];
27
/**
28
     * SmtpTransport constructor.
29
     *
30
     * @param string $host the mail server name or ip address
31
     * @param int $port the mail server port
32 2
     * @param array $options the extra options
33
     */
34 2
    public function __construct($host = 'localhost', $port = 25, $options = [])
35 2
    {
36 2
        $this->host = $host;
37 2
        $this->port = $port;
38
        $this->options = $options;
39
    }
40
41
    /**
42
     * @return EsmtpTransport
43
     */
44 1
    public function getInstance(): EsmtpTransport
45
    {
46 1
        if ($this->instance === null) {
47 1
            $user = $this->options['username'] ?? null;
48 1
            $password = $this->options['password'] ?? null;
49 1
            $this->instance = (new EsmtpTransportFactory())->create(new Dsn('smtp', $this->host, $user, $password, $this->port, $this->options));
50 1
        }
51 1
52
        return $this->instance;
53 1
    }
54
55
    private function getScheme()
0 ignored issues
show
Unused Code introduced by
The method getScheme() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
56
    {
57
        return $this->options['tls']
58
            ? 'smtps'
59
            : 'smtp';
60
    }
61
}
62