Completed
Push — master ( fec9e0...63ebd4 )
by Sudar
01:40
created

EmailLogger::log_email()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 52
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.1474

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
c 2
b 0
f 0
nc 24
nop 1
dl 0
loc 52
ccs 21
cts 25
cp 0.84
crap 6.1474
rs 8.6868

How to fix   Long Method   

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 namespace EmailLog\Core;
2
3
/**
4
 * Log's emails sent through `wp_mail`.
5
 *
6
 * @package EmailLog\Core
7
 * @since   2.0
8
 */
9
class EmailLogger implements Loadie {
10
11
	/**
12
	 * Load the logger.
13
	 */
14
	public function load() {
15
		add_filter( 'wp_mail', array( $this, 'log_email' ) );
16
	}
17
18
	/**
19
	 * Logs email to database.
20
	 *
21
	 * @param array $mail_info Information about email.
22
	 *
23
	 * @return array Information about email.
24
	 */
25 1
	public function log_email( $mail_info ) {
26 1
		$email_log = email_log();
27
		/**
28
		 * Hook to modify wp_mail contents before Email Log plugin logs.
29
		 *
30
		 * @since Genesis
31
		 *
32
		 * @param array $mail_info {
33
		 *     @type string $to
34
		 *     @type string $subject
35
		 *     @type string $message
36
		 *     @type string $headers
37
		 *     @type string $attachment
38
		 * }
39
		 */
40 1
		$mail_info = apply_filters( 'el_wp_mail_log', $mail_info );
41
42
		// Sometimes the array passed to the `wp_mail` filter may not contain all the required keys.
43
		// See https://wordpress.org/support/topic/illegal-string-offset-attachments/
44 1
		$mail_info = wp_parse_args( $mail_info, array(
45 1
			'attachments' => array(),
46 1
			'to'          => '',
47 1
			'subject'     => '',
48 1
			'headers'     => '',
49 1
		) );
50
51
		$data = array(
52 1
			'attachments' => ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false',
53 1
			'to_email'    => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'],
54 1
			'subject'     => $mail_info['subject'],
55 1
			'headers'     => is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'],
56 1
			'sent_date'   => current_time( 'mysql' ),
57 1
		);
58
59 1
		$message = '';
60
61 1
		if ( isset( $mail_info['message'] ) ) {
62
			$message = $mail_info['message'];
63
		} else {
64
			// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20
65
			// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them.
66 1
			if ( isset( $mail_info['html'] ) ) {
67
				$message = $mail_info['html'];
68
			}
69
		}
70
71 1
		$data['message'] = $message;
72
73 1
		$email_log->table_manager->insert_log( $data );
74
75 1
		return $mail_info;
76
	}
77
}
78