Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

SmtpTransportMailerFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 63
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B factory() 0 52 7
1
<?php
2
3
/**
4
 * TechDivision\Import\Loggers\SwiftMailer\SmtpTransportMailerFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Loggers\SwiftMailer;
22
23
use TechDivision\Import\Utils\SwiftMailerKeys;
24
use TechDivision\Import\Configuration\SwiftMailer\TransportConfigurationInterface;
25
26
/**
27
 * Factory implementation for a swift mailer with SMTP transport.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class SmtpTransportMailerFactory implements TransportMailerFactoryInterface
36
{
37
38
    /**
39
     * Creates a new swift mailer instance based on the passed transport configuration.
40
     *
41
     * @param \TechDivision\Import\Configuration\SwiftMailer\TransportConfigurationInterface $transportConfiguration The mailer configuration
42
     *
43
     * @return \Swift_Mailer The mailer instance
44
     */
45
    public function factory(TransportConfigurationInterface $transportConfiguration)
46
    {
47
48
        // load the SMTP host from the configuration
49
        $smtpHost = null;
50
        if ($transportConfiguration->hasParam(SwiftMailerKeys::SMTP_HOST)) {
51
            $smtpHost = $transportConfiguration->getParam(SwiftMailerKeys::SMTP_HOST);
52
        }
53
54
        // load the SMTP port from the configuration
55
        $smtpPort = null;
56
        if ($transportConfiguration->hasParam(SwiftMailerKeys::SMTP_PORT)) {
57
            $smtpPort = $transportConfiguration->getParam(SwiftMailerKeys::SMTP_PORT);
58
        }
59
60
        // load the SMTP security from the configuration
61
        $smtpSecurity = null;
62
        if ($transportConfiguration->hasParam(SwiftMailerKeys::SMTP_SECURITY)) {
63
            $smtpSecurity = $transportConfiguration->getParam(SwiftMailerKeys::SMTP_SECURITY);
64
        }
65
66
        // initialize and create the mailer transport instance
67
        $transport = \Swift_SmtpTransport::newInstance($smtpHost, $smtpPort, $smtpSecurity);
68
69
        // query whether or not if a SMTP authentication mode has been specified
70
        if ($transportConfiguration->hasParam(SwiftMailerKeys::SMTP_AUTH_MODE)) {
71
            // load the authentication mode from the configuration
72
            $transport->setAuthMode($transportConfiguration->getParam(SwiftMailerKeys::SMTP_AUTH_MODE));
73
            // set the stream context options, e. g. to allow self signed certificates
74
            $transport->setStreamOptions(
75
                array(
76
                    'ssl' => array(
77
                        'allow_self_signed' => true,
78
                        'verify_peer'       => false
79
                    )
80
                )
81
            );
82
83
            // load the SMTP username from the configuration
84
            if ($transportConfiguration->hasParam(SwiftMailerKeys::SMTP_USERNAME)) {
85
                $transport->setUsername($transportConfiguration->getParam(SwiftMailerKeys::SMTP_USERNAME));
86
            }
87
88
            // load the SMTP password from the configuration
89
            if ($transportConfiguration->hasParam(SwiftMailerKeys::SMTP_PASSWORD)) {
90
                $transport->setPassword($transportConfiguration->getParam(SwiftMailerKeys::SMTP_PASSWORD));
91
            }
92
        }
93
94
        // initialize, create and return the swift mailer instance
95
        return \Swift_Mailer::newInstance($transport);
96
    }
97
}
98