Swift_Plugin_MailSend::sendPerformed()   F
last analyzed

Complexity

Conditions 11
Paths 768

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 29
c 1
b 0
f 0
nc 768
nop 1
dl 0
loc 41
rs 3.3333

How to fix   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
2
3
/**
4
 * Swift Mailer mail() sending plugin
5
 * Please read the LICENSE file
6
 * @author Chris Corbyn <[email protected]>
7
 * @package Swift_Connection
8
 * @license GNU Lesser General Public License
9
 */
10
11
require_once dirname(__FILE__) . "/../ClassLoader.php";
12
Swift_ClassLoader::load("Swift_Events_SendListener");
13
Swift_ClassLoader::load("Swift_Events_BeforeSendListener");
14
15
/**
16
 * Swift mail() send plugin
17
 * Sends the message using mail() when a SendEvent is fired.  Using the NativeMail connection provides stub responses to allow this to happen cleanly.
18
 * @package Swift_Connection
19
 * @author Chris Corbyn <[email protected]>
20
 */
21
class Swift_Plugin_MailSend implements Swift_Events_SendListener, Swift_Events_BeforeSendListener
22
{
23
  /**
24
   * The operating system of the server
25
   * @var string
26
   */
27
  protected $OS = null;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $OS. 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...
28
  /**
29
   * The return path in use here
30
   * @var string
31
   */
32
  protected $returnPath = null;
33
  /**
34
   * The line ending before we intrusively change it
35
   * @var string
36
   */
37
  protected $oldLE = "\r\n";
38
  /**
39
   * 5th parameter in mail().
40
   * @var string
41
   */
42
  protected $additionalParams;
43
  
44
  /**
45
   * Constructor.
46
   * @param string 5th mail() function parameter as a sprintf() formatted string where %s is the sender.
47
   */
48
  public function __construct($params="-oi -f %s")
49
  {
50
    $this->setAdditionalParams($params);
51
    $this->setOS(PHP_OS);
52
  }
53
  /**
54
   * Set the 5th mail() function parameter as a sprintf() formatted string where %s is the sender.
55
   * @param string
56
   */
57
  public function setAdditionalParams($params)
58
  {
59
    $this->additionalParams = $params;
60
  }
61
  /**
62
   * Get the 5th mail() function parameter as a sprintf() string.
63
   * @return string
64
   */
65
  public function getAdditionalParams()
66
  {
67
    return $this->additionalParams;
68
  }
69
  /**
70
   * Set the operating system string (changes behaviour with LE)
71
   * @param string The operating system
72
   * @param string $os
73
   */
74
  public function setOS($os)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $os. 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...
75
  {
76
    $this->OS = $os;
77
  }
78
  /**
79
   * Get the operating system string
80
   * @return string
81
   */
82
  public function getOS()
83
  {
84
    return $this->OS;
85
  }
86
  /**
87
   * Check if this is windows or not
88
   * @return boolean
89
   */
90
  public function isWindows()
91
  {
92
    return (substr($this->getOS(), 0, 3) == "WIN");
93
  }
94
  /**
95
   * Swift's BeforeSendEvent listener.
96
   * Invoked just before Swift sends a message
97
   * @param Swift_Events_SendEvent The event information
98
   */
99
  public function beforeSendPerformed(Swift_Events_SendEvent $e)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $e. 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...
100
  {
101
    $message = $e->getMessage();
102
    $message->uncacheAll();
103
    $this->oldLE = $message->getLE();
104
    if (!$this->isWindows() && $this->oldLE != "\n") $message->setLE("\n");
105
  }
106
  /**
107
   * Swift's SendEvent listener.
108
   * Invoked when Swift sends a message
109
   * @param Swift_Events_SendEvent The event information
110
   * @throws Swift_ConnectionException If mail() returns false
111
   */
112
  public function sendPerformed(Swift_Events_SendEvent $e)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $e. 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...
113
  {
114
    $message = $e->getMessage();
115
    $recipients = $e->getRecipients();
116
    
117
    $to = array();
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...
118
    foreach ($recipients->getTo() as $addr)
119
    {
120
      if ($this->isWindows()) $to[] = substr($addr->build(true), 1, -1);
121
      else $to[] = $addr->build();
122
    }
123
    $to = implode(", ", $to);
124
    
125
    $bcc_orig = $message->headers->has("Bcc") ? $message->headers->get("Bcc") : null;
126
    $subject_orig = $message->headers->has("Subject") ? $message->headers->get("Subject") : null;
127
    $to_orig = $message->headers->has("To") ? $message->headers->get("To") : null;
128
    
129
    $bcc = array();
130
    foreach ($recipients->getBcc() as $addr) $bcc[] = $addr->build();
131
    if (!empty($bcc)) $message->headers->set("Bcc", $bcc);
132
    $bcc = null;
0 ignored issues
show
Unused Code introduced by
$bcc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
    
134
    $body_data = $message->buildData();
135
    $message_body = $body_data->readFull();
136
    
137
    $subject_enc = $message->headers->has("Subject") ? $message->headers->getEncoded("Subject") : "";
138
    
139
    $message->headers->set("To", null);
140
    $message->headers->set("Subject", null);
141
    
142
    $sender = $e->getSender();
143
    $this->returnPath = $sender->build();
144
    if ($message->headers->has("Return-Path")) $this->returnPath = $message->headers->get("Return-Path");
145
    if (preg_match("~<([^>]+)>[^>]*\$~", $this->returnPath, $matches)) $this->returnPath = $matches[1];
146
    
147
    $this->doMail($to, $subject_enc, $message_body, $message->headers, sprintf($this->getAdditionalParams(), $this->returnPath));
148
    $message->setLE($this->oldLE);
149
    $message->headers->set("To", $to_orig);
150
    $message->headers->set("Subject", $subject_orig);
151
    $message->headers->set("Bcc", $bcc_orig);
152
  }
153
  
154
  /**
155
   * @param string $to
156
   * @param string $subject
157
   * @param string $message
158
   * @param Swift_Message_Headers $headers
159
   * @param string $params
160
   */
161
  public function doMail($to, $subject, $message, $headers, $params)
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...
162
  {
163
    $original_from = @ini_get("sendmail_from");
164
    @ini_set("sendmail_from", $this->returnPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
165
    
166
    $headers = $headers->build();
167
    
168
    if (!ini_get("safe_mode")) $success = mail($to, $subject, $message, $headers, $params);
169
    else $success = mail($to, $subject, $message, $headers);
170
    
171
    if (!$success)
172
    {
173
      @ini_set("sendmail_from", $original_from);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
174
      throw new Swift_ConnectionException("Sending failed using mail() as PHP's default mail() function returned boolean FALSE.");
175
    }
176
    @ini_set("sendmail_from", $original_from);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
177
  }
178
}
179