Completed
Push — master ( de57e8...628785 )
by Nazar
04:42
created

Mail::send_to()   D

Complexity

Conditions 10
Paths 49

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 49
nop 7
dl 0
loc 32
ccs 22
cts 22
cp 1
crap 10
rs 4.8196
c 0
b 0
f 0

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
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	h,
11
	PHPMailer,
12
	phpmailerException;
13
14
/**
15
 * @method static $this instance($check = false)
16
 */
17
class Mail {
18
	use Singleton;
19
	/**
20
	 * Sending of email
21
	 *
22
	 * @param array|string|string[]      $email       if emails without names - string (may be several emails separated by comma) or
23
	 *                                                1-dimensional array(<i>email</i>)<br>
24
	 *                                                2-dimensional of emails or array(<i>email</i>, <i>name</i>) must be given
25
	 * @param string                     $subject     Mail subject
26
	 * @param string                     $body        html body
27
	 * @param string|null                $body_text   plain text body
28
	 * @param array|null|string          $attachments 1- or 2-dimensional array of array(<i>path</i>, <i>name</i>) or simply string with path to the file in
29
	 *                                                file system
30
	 * @param array|null|string|string[] $reply_to    Similar to <b>$email</b>, but multiple emails are not supported
31
	 * @param bool|string                $signature   <b>true</b> - add system signature<br>
32
	 *                                                <b>false</b> - without signature<br>
33
	 *                                                <b>string</b> - custom signature
34
	 *
35
	 * @return bool
36
	 */
37 2
	public function send_to ($email, $subject, $body, $body_text = null, $attachments = null, $reply_to = null, $signature = true) {
38 2
		if (!$email || !$subject || !$body) {
39 2
			return false;
40
		}
41 2
		$PHPMailer = $this->phpmailer_instance();
42 2
		foreach ($this->normalize_email($email) as $e) {
43 2
			$PHPMailer->addAddress(...$e);
44
		}
45 2
		foreach ($this->normalize_email($reply_to) as $r) {
46 2
			$PHPMailer->addReplyTo(...$r);
47
		}
48 2
		foreach ($this->normalize_attachment($attachments) as $a) {
49
			try {
50 2
				$PHPMailer->addAttachment(...$a);
51 2
			} catch (phpmailerException $e) {
52 2
				trigger_error($e->getMessage(), E_USER_WARNING);
53
			}
54
		}
55 2
		$PHPMailer->Subject = $subject;
56 2
		$signature          = $this->make_signature($signature);
57 2
		$PHPMailer->Body    = $this->normalize_body($body, $signature);
58 2
		if ($body_text) {
59 2
			$PHPMailer->AltBody = $body_text.strip_tags($signature);
60
		}
61
		try {
62 2
			$result = $PHPMailer->send();
63 2
		} catch (phpmailerException $e) {
64 2
			trigger_error($e->getMessage(), E_USER_WARNING);
65 2
			$result = false;
66
		}
67 2
		return $result;
68
	}
69
	/**
70
	 * Create PHPMailer instance with parameters according to system configuration
71
	 *
72
	 * @return PHPMailer
73
	 */
74 2
	protected function phpmailer_instance () {
75 2
		$PHPMailer = new PHPMailer(true);
76 2
		$Config    = Config::instance();
77 2
		if ($Config->core['smtp']) {
78 2
			$PHPMailer->isSMTP();
79 2
			$PHPMailer->Host       = $Config->core['smtp_host'];
80 2
			$PHPMailer->Port       = $Config->core['smtp_port'];
81 2
			$PHPMailer->SMTPSecure = $Config->core['smtp_secure'];
82 2
			if ($Config->core['smtp_auth']) {
83 2
				$PHPMailer->SMTPAuth = true;
84 2
				$PHPMailer->Username = $Config->core['smtp_user'];
85 2
				$PHPMailer->Password = $Config->core['smtp_password'];
86
			}
87
		}
88 2
		$PHPMailer->From     = $Config->core['mail_from'];
89 2
		$PHPMailer->FromName = get_core_ml_text('mail_from_name');
90 2
		$PHPMailer->CharSet  = 'utf-8';
91 2
		$PHPMailer->isHTML();
92 2
		return $PHPMailer;
93
	}
94
	/**
95
	 * @param array|string|string[] $email
96
	 *
97
	 * @return string[][]
98
	 */
99 2
	protected function normalize_email ($email) {
100 2
		if (!$email) {
101 2
			return [];
102
		}
103 2
		if (!is_array($email)) {
104 2
			$email = _trim(explode(',', $email));
105 2
		} elseif (!is_array($email[0])) {
106 2
			$email = [$email];
107
		}
108 2
		return _array($email);
109
	}
110 2
	protected function make_signature ($signature) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
111 2
		if ($signature === true) {
112 2
			$signature = get_core_ml_text('mail_signature');
113 2
			return $signature ? "<br>\n<br>\n-- \n<br>$signature" : '';
114
		}
115 2
		return $signature ? "<br>\n<br>\n-- \n<br>".xap($signature, true) : '';
116
	}
117
	/**
118
	 * @param string $body
119
	 * @param string $signature
120
	 *
121
	 * @return string
122
	 */
123 2
	protected function normalize_body ($body, $signature) {
124 2
		if (strpos($body, '<html') === false) {
125 2
			if (strpos($body, '<body') === false) {
126 2
				$body = h::body($body.$signature);
127
			} else {
128 2
				$body = str_replace('</body>', "$signature</body>", $body);
129
			}
130 2
			$body = h::html(
131 2
				h::{'head meta'}(
132
					[
133 2
						'content'    => 'text/html; charset=utf-8',
134
						'http-equiv' => 'Content-Type'
135
					]
136
				).
137 2
				$body
138
			);
139
		} else {
140 2
			$body = str_replace('</body>', "$signature</body>", $body);
141
		}
142 2
		if (strpos($body, '<!doctype') === false) {
143 2
			$body = "<!doctype html>\n$body";
144
		}
145 2
		return $body;
146
	}
147
	/**
148
	 * @param array|null|string $attachments
149
	 *
150
	 * @return array
151
	 */
152 2
	protected function normalize_attachment ($attachments) {
153 2
		if (!$attachments) {
154 2
			return [];
155
		}
156 2
		if (!is_array($attachments) || !is_array($attachments[0])) {
157 2
			$attachments = [$attachments];
158
		}
159 2
		return _array($attachments);
160
	}
161
}
162