Total Complexity | 40 |
Total Lines | 410 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 1 | Features | 0 |
Complex classes like SMTPGateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SMTPGateway, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class SMTPGateway extends EmailGateway |
||
14 | { |
||
15 | protected $_SMTP; |
||
16 | protected $_helo_hostname; |
||
17 | protected $_host; |
||
18 | protected $_port; |
||
19 | protected $_protocol = 'tcp'; |
||
20 | protected $_secure = 'no'; |
||
21 | protected $_auth = false; |
||
22 | protected $_user; |
||
23 | protected $_pass; |
||
24 | protected $_envelope_from; |
||
25 | |||
26 | /** |
||
27 | * Returns the name, used in the dropdown menu in the preferences pane. |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | public static function about() |
||
32 | { |
||
33 | return array( |
||
34 | 'name' => __('SMTP'), |
||
35 | ); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Constructor. Sets basic default values based on preferences. |
||
40 | * |
||
41 | * @throws EmailValidationException |
||
42 | */ |
||
43 | public function __construct() |
||
44 | { |
||
45 | parent::__construct(); |
||
46 | $this->setConfiguration(Symphony::Configuration()->get('email_smtp')); |
||
|
|||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Send an email using an SMTP server |
||
51 | * |
||
52 | * @throws EmailGatewayException |
||
53 | * @throws EmailValidationException |
||
54 | * @throws Exception |
||
55 | * @return boolean |
||
56 | */ |
||
57 | public function send() |
||
58 | { |
||
59 | $this->validate(); |
||
60 | |||
61 | $settings = array(); |
||
62 | $settings['helo_hostname'] = $this->_helo_hostname; |
||
63 | if ($this->_auth) { |
||
64 | $settings['username'] = $this->_user; |
||
65 | $settings['password'] = $this->_pass; |
||
66 | } |
||
67 | $settings['secure'] = $this->_secure; |
||
68 | |||
69 | try { |
||
70 | if (!is_a($this->_SMTP, 'SMTP')) { |
||
71 | $this->_SMTP = new SMTP($this->_host, $this->_port, $settings); |
||
72 | } |
||
73 | |||
74 | // Encode recipient names (but not any numeric array indexes) |
||
75 | $recipients = array(); |
||
76 | foreach ($this->_recipients as $name => $email) { |
||
77 | // Support Bcc header |
||
78 | if (isset($this->_header_fields['Bcc']) && $this->_header_fields['Bcc'] == $email) { |
||
79 | continue; |
||
80 | } |
||
81 | |||
82 | // if the key is not numeric, qEncode the key. |
||
83 | $name = General::intval($name) > -1 ? General::intval($name) : EmailHelper::qEncode($name); |
||
84 | $recipients[$name] = $email; |
||
85 | } |
||
86 | |||
87 | // Combine keys and values into a recipient list (name <email>, name <email>). |
||
88 | $recipient_list = EmailHelper::arrayToList($recipients); |
||
89 | |||
90 | // Encode the subject |
||
91 | $subject = EmailHelper::qEncode((string)$this->_subject); |
||
92 | |||
93 | // Build the 'From' header field body |
||
94 | $from = empty($this->_sender_name) |
||
95 | ? $this->_sender_email_address |
||
96 | : EmailHelper::qEncode($this->_sender_name) . ' <' . $this->_sender_email_address . '>'; |
||
97 | |||
98 | // Build the 'Reply-To' header field body |
||
99 | if (!empty($this->_reply_to_email_address)) { |
||
100 | $reply_to = empty($this->_reply_to_name) |
||
101 | ? $this->_reply_to_email_address |
||
102 | : EmailHelper::qEncode($this->_reply_to_name) . ' <'.$this->_reply_to_email_address.'>'; |
||
103 | } |
||
104 | |||
105 | if (!empty($reply_to)) { |
||
106 | $this->_header_fields = array_merge( |
||
107 | $this->_header_fields, |
||
108 | array( |
||
109 | 'Reply-To' => $reply_to, |
||
110 | ) |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | // Build the body text using attachments, html-text and plain-text. |
||
115 | $this->prepareMessageBody(); |
||
116 | |||
117 | // Build the header fields |
||
118 | $this->_header_fields = array_merge( |
||
119 | $this->_header_fields, |
||
120 | array( |
||
121 | 'Message-ID' => sprintf('<%s@%s>', md5(uniqid()), HTTP_HOST), |
||
122 | 'Date' => date('r'), |
||
123 | 'From' => $from, |
||
124 | 'Subject' => $subject, |
||
125 | 'To' => $recipient_list, |
||
126 | 'X-Mailer' => 'Symphony Email Module', |
||
127 | 'MIME-Version' => '1.0' |
||
128 | ) |
||
129 | ); |
||
130 | |||
131 | // Set header fields and fold header field bodies |
||
132 | foreach ($this->_header_fields as $name => $body) { |
||
133 | $this->_SMTP->setHeader($name, EmailHelper::fold($body)); |
||
134 | } |
||
135 | |||
136 | // Send the email command. If the envelope from variable is set, use that for the MAIL command. This improves bounce handling. |
||
137 | $this->_SMTP->sendMail(is_null($this->_envelope_from)?$this->_sender_email_address:$this->_envelope_from, $this->_recipients, $this->_body); |
||
138 | |||
139 | if ($this->_keepalive === false) { |
||
140 | $this->closeConnection(); |
||
141 | } |
||
142 | |||
143 | $this->reset(); |
||
144 | } catch (SMTPException $e) { |
||
145 | throw new EmailGatewayException($e->getMessage()); |
||
146 | } |
||
147 | |||
148 | return true; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Resets the headers, body, subject |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | public function reset() |
||
157 | { |
||
158 | $this->_header_fields = array(); |
||
159 | $this->_envelope_from = null; |
||
160 | $this->_recipients = array(); |
||
161 | $this->_subject = null; |
||
162 | $this->_body = null; |
||
163 | } |
||
164 | |||
165 | public function openConnection() |
||
166 | { |
||
167 | return parent::openConnection(); |
||
168 | } |
||
169 | |||
170 | public function closeConnection() |
||
171 | { |
||
172 | if (is_a($this->_SMTP, 'SMTP')) { |
||
173 | try { |
||
174 | $this->_SMTP->quit(); |
||
175 | return parent::closeConnection(); |
||
176 | } catch (Exception $e) { |
||
177 | } |
||
178 | } |
||
179 | |||
180 | parent::closeConnection(); |
||
181 | return false; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Sets the HELO/EHLO hostanme |
||
186 | * |
||
187 | * @param string $helo_hostname |
||
188 | * @return void |
||
189 | */ |
||
190 | public function setHeloHostname($helo_hostname = null) |
||
191 | { |
||
192 | $this->_helo_hostname = $helo_hostname; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Sets the host to connect to. |
||
197 | * |
||
198 | * @param null|string $host (optional) |
||
199 | * @return void |
||
200 | */ |
||
201 | public function setHost($host = null) |
||
202 | { |
||
203 | if ($host === null) { |
||
204 | $host = '127.0.0.1'; |
||
205 | } |
||
206 | |||
207 | if (substr($host, 0, 6) == 'ssl://') { |
||
208 | $this->_protocol = 'ssl'; |
||
209 | $this->_secure = 'ssl'; |
||
210 | $host = substr($host, 6); |
||
211 | } |
||
212 | $this->_host = $host; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Sets the port, used in the connection. |
||
217 | * |
||
218 | * @param null|int $port |
||
219 | * @return void |
||
220 | */ |
||
221 | public function setPort($port = null) |
||
222 | { |
||
223 | if (is_null($port)) { |
||
224 | $port = ($this->_protocol == 'ssl') ? 465 : 25; |
||
225 | } |
||
226 | |||
227 | $this->_port = $port; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Sets the username to use with AUTH LOGIN |
||
232 | * |
||
233 | * @param string $user |
||
234 | * @return void |
||
235 | */ |
||
236 | public function setUser($user = null) |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Sets the password to use with AUTH LOGIN |
||
243 | * |
||
244 | * @param string $pass |
||
245 | * @return void |
||
246 | */ |
||
247 | public function setPass($pass = null) |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Use AUTH login or no auth. |
||
254 | * |
||
255 | * @param boolean $auth |
||
256 | * @return void |
||
257 | */ |
||
258 | public function setAuth($auth = false) |
||
259 | { |
||
260 | $this->_auth = $auth; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Sets the encryption used. |
||
265 | * |
||
266 | * @param string $secure |
||
267 | * The encryption used. Can be 'ssl', 'tls'. Anything else defaults to |
||
268 | * a non secure TCP connection |
||
269 | * @return void |
||
270 | */ |
||
271 | public function setSecure($secure = null) |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Sets the envelope_from address. This is only available via the API, as it is an expert-only feature. |
||
287 | * |
||
288 | * @since 2.3.1 |
||
289 | * @param null $envelope_from |
||
290 | * @throws EmailValidationException |
||
291 | * @return void |
||
292 | */ |
||
293 | public function setEnvelopeFrom($envelope_from = null) |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Sets all configuration entries from an array. |
||
304 | * |
||
305 | * @param array $config |
||
306 | * All configuration entries stored in a single array. |
||
307 | * The array should have the format of the $_POST array created by the preferences HTML. |
||
308 | * @throws EmailValidationException |
||
309 | * @since 2.3.1 |
||
310 | * @return void |
||
311 | */ |
||
312 | public function setConfiguration($config) |
||
313 | { |
||
314 | $this->setHeloHostname($config['helo_hostname']); |
||
315 | $this->setFrom($config['from_address'], $config['from_name']); |
||
316 | $this->setHost($config['host']); |
||
317 | $this->setPort($config['port']); |
||
318 | $this->setSecure($config['secure']); |
||
319 | |||
320 | $this->setAuth((int)$config['auth'] === 1); |
||
321 | $this->setUser($config['username']); |
||
322 | $this->setPass($config['password']); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Builds the preferences pane, shown in the Symphony backend. |
||
327 | * |
||
328 | * @throws InvalidArgumentException |
||
329 | * @return XMLElement |
||
330 | */ |
||
331 | public function getPreferencesPane() |
||
423 | } |
||
424 | } |
||
425 |