Completed
Pull Request — master (#1458)
by Tim
32:04
created

MailFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 13 4
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure;
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 4
				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
}