MailerFactory   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 83
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A getSwiftMailerFromConfig() 0 28 4
B getMailConfig() 0 21 7
1
<?php
2
3
namespace Fabrica\Helper;
4
5
/**
6
 * Class MailerFactory helps to set up and configure a SwiftMailer object.
7
 */
8
class MailerFactory
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $emailConfig;
14
15
    /**
16
     * Set the mailer factory configuration.
17
     *
18
     * @param array $config
19
     */
20
    public function __construct($config = [])
21
    {
22
        if (!is_array($config)) {
23
            $config = [];
24
        }
25
26
        $this->emailConfig  = isset($config['email_settings']) ? $config['email_settings'] : [];
27
    }
28
29
    /**
30
     * Returns an instance of Swift_Mailer based on the config.s
31
     *
32
     * @return \Swift_Mailer
33
     */
34
    public function getSwiftMailerFromConfig()
35
    {
36
        if ($this->getMailConfig('smtp_address')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getMailConfig('smtp_address') of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
37
            $encryptionType = $this->getMailConfig('smtp_encryption');
38
39
            // Workaround issue where smtp_encryption could == 1 in the past by
40
            // checking it is a valid transport
41
            if ($encryptionType && !in_array($encryptionType, stream_get_transports())) {
42
                $encryptionType = null;
43
            }
44
45
            /**
46
 * @var \Swift_SmtpTransport $transport 
47
*/
48
            $transport = \Swift_SmtpTransport::newInstance(
0 ignored issues
show
Bug introduced by
The method newInstance() does not seem to exist on object<Swift_SmtpTransport>.

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...
49
                $this->getMailConfig('smtp_address'),
50
                $this->getMailConfig('smtp_port'),
51
                $encryptionType
52
            );
53
54
            $transport->setUsername($this->getMailConfig('smtp_username'));
55
            $transport->setPassword($this->getMailConfig('smtp_password'));
56
        } else {
57
            $transport = \Swift_MailTransport::newInstance(null);
58
        }
59
60
        return \Swift_Mailer::newInstance($transport);
0 ignored issues
show
Bug introduced by
The method newInstance() does not seem to exist on object<Swift_Mailer>.

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...
61
    }
62
63
    /**
64
     * Return a specific configuration value by key.
65
     *
66
     * @param  $configName
67
     * @return null|string
68
     */
69
    public function getMailConfig($configName)
70
    {
71
        if (isset($this->emailConfig[$configName]) && $this->emailConfig[$configName] != "") {
72
            return $this->emailConfig[$configName];
73
        } else {
74
            // Check defaults
75
76
            switch ($configName) {
77
            case 'smtp_address':
78
                return "";
79
            case 'default_mailto_address':
80
                return null;
81
            case 'smtp_port':
82
                return '25';
83
            case 'smtp_encryption':
84
                return null;
85
            default:
86
                return "";
87
            }
88
        }
89
    }
90
}
91