Swift3   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 24 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 2
dl 12
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getMailer() 12 22 4
A getMessage() 0 14 2
A send() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Email/Swift3.php
9
 */
10
namespace mithra62\Email;
11
12
/**
13
 * Jaeger - Swift3 Email Abstraction
14
 *
15
 * Defines what email objects should contain
16
 *
17
 * @package Email
18
 * @author Eric Lamb <[email protected]>
19
 */
20
class Swift3 extends SwiftAbstract
21
{
22
    public function __construct($config = array())
23
    {
24
        $this->config = $config;
25
    }
26
    
27
    public function getMailer()
28
    {
29
        if(is_null($this->mailer))
30
        {
31 View Code Duplication
            if (isset($this->config['type']) && $this->config['type'] == 'smtp') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
33
                $transport = new \Swift_Connection_SMTP(
34
                    $this->config['smtp_options']['host'],
35
                    $this->config['smtp_options']['port']
36
                );
37
                
38
                $transport->setUsername($this->config['smtp_options']['connection_config']['username']);
39
                $transport->setPassword($this->config['smtp_options']['connection_config']['password']);
40
            } else {
41
                $transport = new \Swift_Connection_NativeMail();
42
            }
43
            
44
            $this->mailer = new \Swift($transport);
0 ignored issues
show
Unused Code introduced by
The call to Swift::__construct() has too many arguments starting with $transport.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
        }
46
        
47
        return $this->mailer;
48
    }
49
    
50
    public function getMessage(array $to, $from_email, $from_name, $subject, $message_body, array $attachments, $mail_type='html')
51
    {
52
        $to_list = new \Swift_RecipientList();
53
        foreach ($to as $key => $addr) {
54
            $to_list->addTo($addr);
55
        }
56
        
57
        $this->to_list = $to_list;
58
        $this->from_email = $from_email;
59
        $this->from_name = $from_name;
60
        
61
        $message = new \Swift_Message($subject, $message_body, "text/".$mail_type);
62
        return $message;
63
    }
64
    
65
    public function send($message, $extra = null)
66
    {
67
        return $this->getMailer()->send($message, $this->to_list, new \Swift_Address($this->from_email, $this->from_name));
68
    }
69
}