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

GreetingGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Presentation;
6
7
use Symfony\Component\Translation\TranslatorInterface;
8
9
/**
10
 * @license GNU GPL v2+
11
 */
12
class GreetingGenerator {
13
14
	private const GREETING_MALE = 'Herr';
15
	private const GREETING_FEMALE = 'Frau';
16
	private const GREETING_FAMILY = 'Familie';
17
18
	private $translator;
19
20 109
	public function __construct( TranslatorInterface $translator ) {
21 109
		$this->translator = $translator;
22 109
	}
23
24 65
	public function createFormalGreeting( string $lastName, string $salutation, string $title ): string {
25 65
		if ( $lastName === '' ) {
26 3
			return $this->translator->trans( 'mail_introduction_generic' );
27
		}
28
29 62
		$spacedTitle = $title === '' ? '' : $title . ' ';
30
31
		switch ( $salutation ) {
32 62
			case self::GREETING_MALE:
33 37
				return $this->translator->trans( 'mail_introduction_male_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
34 43
			case self::GREETING_FEMALE:
35 33
				return $this->translator->trans( 'mail_introduction_female_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
36 10
			case self::GREETING_FAMILY:
37 1
				return $this->translator->trans( 'mail_introduction_family_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
38
			default:
39 9
				return $this->translator->trans( 'mail_introduction_generic' );
40
		}
41
	}
42
43 24
	public function createInformalGreeting( string $salutation, string $firstName, string $lastName ): string {
44 24
		if ( ( $salutation !== self::GREETING_FAMILY && $firstName === '' ) ||
45 24
			( $salutation === self::GREETING_FAMILY && $lastName === '' ) ) {
46 2
			return $this->translator->trans( 'mail_introduction_generic' );
47
		}
48
49
		switch ( $salutation ) {
50 22
			case self::GREETING_MALE:
51 19
				return $this->translator->trans( 'mail_introduction_male_informal', [ '%firstName%' => $firstName ] );
52 3
			case self::GREETING_FEMALE:
53 1
				return $this->translator->trans( 'mail_introduction_female_informal', [ '%firstName%' => $firstName ] );
54 2
			case self::GREETING_FAMILY:
55 1
				return $this->translator->trans( 'mail_introduction_family_informal', [ '%lastName%' => $lastName ] );
56
			default:
57 1
				return $this->translator->trans( 'mail_introduction_generic' );
58
		}
59
	}
60
}