email_Core::connect()   D
last analyzed

Complexity

Conditions 16
Paths 392

Size

Total Lines 73
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 16
eloc 34
c 2
b 1
f 0
nc 392
nop 1
dl 0
loc 73
rs 4.0751

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * Email helper class.
4
 *
5
 * $Id: email.php 3769 2008-12-15 00:48:56Z zombor $
6
 *
7
 * @package    Core
8
 * @author     Kohana Team
9
 * @copyright  (c) 2007-2008 Kohana Team
10
 * @license    http://kohanaphp.com/license.html
11
 */
12
class email_Core
13
{
14
15
    // SwiftMailer instance
16
    protected static $mail;
17
18
    /**
19
     * Creates a SwiftMailer instance.
20
     *
21
     * @param   string  DSN connection string
22
     * @return  Swift  Swift object
23
     */
24
    public static function connect($config = null)
25
    {
26
        if (! class_exists('Swift', false)) {
27
            // Load SwiftMailer
28
            require Kohana::find_file('vendor', 'swift/Swift');
29
30
            // Register the Swift ClassLoader as an autoload
31
            spl_autoload_register(array('Swift_ClassLoader', 'load'));
32
        }
33
34
        // Load default configuration
35
        ($config === null) and $config = Kohana::config('email');
36
37
        switch ($config['driver']) {
38
            case 'smtp':
39
                // Set port
40
                $port = empty($config['options']['port']) ? null : (int) $config['options']['port'];
41
42
                if (empty($config['options']['encryption'])) {
43
                    // No encryption
44
                    $encryption = Swift_Connection_SMTP::ENC_OFF;
45
                } else {
46
                    // Set encryption
47
                    switch (strtolower($config['options']['encryption'])) {
48
                        case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break;
49
                        case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break;
50
                    }
51
                }
52
53
                // Create a SMTP connection
54
                $connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption);
0 ignored issues
show
Bug introduced by
The variable $encryption does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
55
56
                // Do authentication, if part of the DSN
57
                empty($config['options']['username']) or $connection->setUsername($config['options']['username']);
58
                empty($config['options']['password']) or $connection->setPassword($config['options']['password']);
59
60
                if (! empty($config['options']['auth'])) {
61
                    // Get the class name and params
62
                    list($class, $params) = arr::callback_string($config['options']['auth']);
63
64
                    if ($class === 'PopB4Smtp') {
65
                        // Load the PopB4Smtp class manually, due to its odd filename
66
                        require Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$');
67
                    }
68
69
                    // Prepare the class name for auto-loading
70
                    $class = 'Swift_Authenticator_'.$class;
71
72
                    // Attach the authenticator
73
                    $connection->attachAuthenticator(($params === null) ? new $class : new $class($params[0]));
74
                }
75
76
                // Set the timeout to 5 seconds
77
                $connection->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']);
78
            break;
79
            case 'sendmail':
80
                // Create a sendmail connection
81
                $connection = new Swift_Connection_Sendmail(
82
                    empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options']
83
                );
84
85
                // Set the timeout to 5 seconds
86
                $connection->setTimeout(5);
87
            break;
88
            default:
89
                // Use the native connection
90
                $connection = new Swift_Connection_NativeMail($config['options']);
91
            break;
92
        }
93
94
        // Create the SwiftMailer instance
95
        return email::$mail = new Swift($connection);
96
    }
97
98
    /**
99
     * Send an email message.
100
     *
101
     * @param   string|array  recipient email (and name), or an array of To, Cc, Bcc names
102
     * @param   string|array  sender email (and name)
103
     * @param   string        message subject
104
     * @param   string        message body
105
     * @param   boolean       send email as HTML
106
     * @return  integer       number of emails sent
107
     */
108
    public static function send($to, $from, $subject, $message, $html = false)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $to. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
109
    {
110
        // Connect to SwiftMailer
111
        (email::$mail === null) and email::connect();
112
113
        // Determine the message type
114
        $html = ($html === true) ? 'text/html' : 'text/plain';
115
116
        // Create the message
117
        $message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8');
118
119
        if (is_string($to)) {
120
            // Single recipient
121
            $recipients = new Swift_Address($to);
122
        } elseif (is_array($to)) {
123
            if (isset($to[0]) and isset($to[1])) {
124
                // Create To: address set
125
                $to = array('to' => $to);
126
            }
127
128
            // Create a list of recipients
129
            $recipients = new Swift_RecipientList;
130
131
            foreach ($to as $method => $set) {
132
                if (! in_array($method, array('to', 'cc', 'bcc'))) {
133
                    // Use To: by default
134
                    $method = 'to';
135
                }
136
137
                // Create method name
138
                $method = 'add'.ucfirst($method);
139
140
                if (is_array($set)) {
141
                    // Add a recipient with name
142
                    $recipients->$method($set[0], $set[1]);
143
                } else {
144
                    // Add a recipient without name
145
                    $recipients->$method($set);
146
                }
147
            }
148
        }
149
150
        if (is_string($from)) {
151
            // From without a name
152
            $from = new Swift_Address($from);
153
        } elseif (is_array($from)) {
154
            // From with a name
155
            $from = new Swift_Address($from[0], $from[1]);
156
        }
157
158
        return email::$mail->send($message, $recipients, $from);
0 ignored issues
show
Bug introduced by
The variable $recipients does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
159
    }
160
} // End email
161