Completed
Pull Request — master (#1473)
by Tim
31:22 queued 01:19
created

MailFormatter::format()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure\Mail;
6
7
/**
8
 * Trim whitespace for each line and multiple blank lines.
9
 *
10
 * The text can force explicit line breaks with a literal `\n` char sequence.
11
 *
12
 * This is for cleaning up the output of complicated mail templates that have
13
 * structural indentations that are irrelevant for the final output.
14
 *
15
 * @package WMDE\Fundraising\Frontend\Infrastructure
16
 */
17
class MailFormatter {
18
19 66
	public static function format( string $message ): string {
20 66
		$formattedMessage = '';
21 66
		$previousLine = '';
22 66
		foreach ( explode( "\n", $message ) as $line ) {
23 66
			if ( trim( $line ) === '' && $previousLine === '' ) {
24 12
				continue;
25
			}
26 66
			$line = trim( $line );
27 66
			$line = str_replace( '\\n', "\n", $line );
28 66
			$formattedMessage .= $line . "\n";
29 66
			$previousLine = $line;
30
		}
31 66
		return $formattedMessage;
32
	}
33
}