Completed
Pull Request — master (#1611)
by Gabriel
106:18 queued 41:13
created

GreetingGenerator::getSpacedTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 2
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 115
	public function __construct( TranslatorInterface $translator ) {
21 115
		$this->translator = $translator;
22 115
	}
23
24 33
	private static function getSpacedTitle( string $title ): string {
25 33
		$spacedTitle = $title === '' ? '' : $title . ' ';
26 1
		return $spacedTitle;
27
	}
28
29 32
	public function createFormalGreeting( string $lastName, string $salutation, string $title ): string {
30
		if ( $lastName === '' ) {
31
			return $this->translator->trans( 'mail_introduction_generic' );
32 32
		}
33 26
34 6
		$spacedTitle = self::getSpacedTitle( $title );
35 1
36 5
		switch ( $salutation ) {
37 1
			case self::GREETING_MALE:
38
				return $this->translator->trans( 'mail_introduction_male_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
39 4
			case self::GREETING_FEMALE:
40
				return $this->translator->trans( 'mail_introduction_female_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
41
			case self::GREETING_FAMILY:
42
				return $this->translator->trans( 'mail_introduction_family_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
43 25
			default:
44 25
				return $this->translator->trans( 'mail_introduction_generic' );
45 25
		}
46 2
	}
47
48
	public function createInformalGreeting( string $salutation, string $firstName, string $lastName ): string {
49
		if ( ( $salutation !== self::GREETING_FAMILY && $firstName === '' ) ||
50 23
			( $salutation === self::GREETING_FAMILY && $lastName === '' ) ) {
51 20
			return $this->translator->trans( 'mail_introduction_generic' );
52 3
		}
53 1
54 2
		switch ( $salutation ) {
55 1
			case self::GREETING_MALE:
56
				return $this->translator->trans( 'mail_introduction_male_informal', [ '%firstName%' => $firstName ] );
57 1
			case self::GREETING_FEMALE:
58
				return $this->translator->trans( 'mail_introduction_female_informal', [ '%firstName%' => $firstName ] );
59
			case self::GREETING_FAMILY:
60
				return $this->translator->trans( 'mail_introduction_family_informal', [ '%lastName%' => $lastName ] );
61 57
			default:
62 57
				return $this->translator->trans( 'mail_introduction_generic' );
63 3
		}
64
	}
65
66
	public function createInformalLastnameGreeting( string $salutation, string $lastName, string $title ): string {
67 54
		if ( $lastName === '' ) {
68 32
			return $this->translator->trans( 'mail_introduction_generic' );
69 41
		}
70 34
71 7
		$spacedTitle = self::getSpacedTitle( $title );
72 1
73
		switch ( $salutation ) {
74 6
			case self::GREETING_MALE:
75
				return $this->translator->trans( 'mail_introduction_male_lastname_informal', [
76
					'%spacedTitle%' => $spacedTitle,
77
					'%lastName%' => $lastName
78
				] );
79
			case self::GREETING_FEMALE:
80
				return $this->translator->trans( 'mail_introduction_female_lastname_informal', [
81
					'%spacedTitle%' => $spacedTitle,
82
					'%lastName%' => $lastName
83
				] );
84
			case self::GREETING_FAMILY:
85
				return $this->translator->trans( 'mail_introduction_family_informal', [
86
					'%spacedTitle%' => $spacedTitle,
87
					'%lastName%' => $lastName
88
				] );
89
			default:
90
				return $this->translator->trans( 'mail_introduction_generic' );
91
		}
92
	}
93
}