Completed
Pull Request — master (#2923)
by
unknown
21:02 queued 15:53
created

TransportFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 6
dl 0
loc 13
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Common\Mailer;
4
5
use Swift_SendmailTransport;
6
use Swift_SmtpTransport;
7
use Swift_Transport;
8
9
/**
10
 * This class will create the right mailer transport based on some parameters
11
 */
12
class TransportFactory
13
{
14
    /**
15
     * Create The right transport instance based on some settings
16
     *
17
     * @param string $type
18
     * @param string $server
19
     * @param int $port
20
     * @param string $user
21
     * @param string $pass
22
     * @param string $encryption
23
     *
24
     * @return Swift_Transport
25
     */
26 81
    public static function create(
27
        string $type = 'sendmail',
28
        string $server = null,
29
        int $port = 25,
30
        string $user = null,
31
        string $pass = null,
32
        string $encryption = null
33
    ): Swift_Transport {
34 81
        if ($type === 'smtp') {
35
            return self::getSmtpTransport($server, $port, $user, $pass, $encryption);
36
        }
37
38 81
        return self::getMailTransport();
39
    }
40
41
    private static function getSmtpTransport(
42
        string $server = null,
43
        string $port = null,
44
        string $user = null,
45
        string $pass = null,
46
        string $encryption = null
47
    ): Swift_SmtpTransport {
48
        $transport = new Swift_SmtpTransport($server, $port);
0 ignored issues
show
Bug introduced by
It seems like $port can also be of type string; however, parameter $port of Swift_SmtpTransport::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        $transport = new Swift_SmtpTransport($server, /** @scrutinizer ignore-type */ $port);
Loading history...
49
        $transport
50
            ->setUsername($user)
51
            ->setPassword($pass);
52
53
        if (in_array($encryption, ['ssl', 'tls'], true)) {
54
            $transport->setEncryption($encryption);
55
        }
56
57
        return $transport;
58
    }
59
60 81
    private static function getMailTransport(): Swift_SendmailTransport
61
    {
62 81
        return new Swift_SendmailTransport();
63
    }
64
}
65