Passed
Push — master ( 0c8e41...e2099b )
by jelmer
23:42 queued 17:59
created

TransportFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 6
dl 0
loc 13
ccs 4
cts 4
cp 1
crap 2
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 10
    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 10
        if ($type === 'smtp') {
35 3
            return self::getSmtpTransport($server, $port, $user, $pass, $encryption);
36
        }
37
38 7
        return self::getMailTransport();
39
    }
40
41 3
    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 3
        $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 3
            ->setUsername($user)
51 3
            ->setPassword($pass);
52
53 3
        if (in_array($encryption, ['ssl', 'tls'], true)) {
54 1
            $transport->setEncryption($encryption);
55
        }
56
57 3
        return $transport;
58
    }
59
60 7
    private static function getMailTransport(): Swift_SendmailTransport
61
    {
62 7
        return new Swift_SendmailTransport();
63
    }
64
}
65