Passed
Pull Request — master (#1750)
by Gabriel
66:53 queued 01:47
created

GreetingGenerator   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
c 0
b 0
f 0
dl 0
loc 79
rs 10
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSpacedTitle() 0 3 2
A __construct() 0 2 1
A createInformalLastnameGreeting() 0 25 5
B createInformalGreeting() 0 15 8
A createFormalGreeting() 0 16 5
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure\Translation;
6
7
/**
8
 * @license GNU GPL v2+
9
 */
10
class GreetingGenerator {
11
12
	private const GREETING_MALE = 'Herr';
13
	private const GREETING_FEMALE = 'Frau';
14
	private const GREETING_FAMILY = 'Familie';
15
16
	private TranslatorInterface $translator;
17
18
	public function __construct( TranslatorInterface $translator ) {
19
		$this->translator = $translator;
20
	}
21
22
	private static function getSpacedTitle( string $title ): string {
23
		$spacedTitle = $title === '' ? '' : $title . ' ';
24
		return $spacedTitle;
25
	}
26
27
	public function createFormalGreeting( string $lastName, string $salutation, string $title ): string {
28
		if ( $lastName === '' ) {
29
			return $this->translator->trans( 'mail_introduction_generic' );
30
		}
31
32
		$spacedTitle = self::getSpacedTitle( $title );
33
34
		switch ( $salutation ) {
35
			case self::GREETING_MALE:
36
				return $this->translator->trans( 'mail_introduction_male_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
37
			case self::GREETING_FEMALE:
38
				return $this->translator->trans( 'mail_introduction_female_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
39
			case self::GREETING_FAMILY:
40
				return $this->translator->trans( 'mail_introduction_family_formal', [ '%spacedTitle%' => $spacedTitle, '%lastName%' => $lastName ] );
41
			default:
42
				return $this->translator->trans( 'mail_introduction_generic' );
43
		}
44
	}
45
46
	public function createInformalGreeting( string $salutation, string $firstName, string $lastName ): string {
47
		if ( ( $salutation !== self::GREETING_FAMILY && $firstName === '' ) ||
48
			( $salutation === self::GREETING_FAMILY && $lastName === '' ) ) {
49
			return $this->translator->trans( 'mail_introduction_generic' );
50
		}
51
52
		switch ( $salutation ) {
53
			case self::GREETING_MALE:
54
				return $this->translator->trans( 'mail_introduction_male_informal', [ '%firstName%' => $firstName ] );
55
			case self::GREETING_FEMALE:
56
				return $this->translator->trans( 'mail_introduction_female_informal', [ '%firstName%' => $firstName ] );
57
			case self::GREETING_FAMILY:
58
				return $this->translator->trans( 'mail_introduction_family_informal', [ '%lastName%' => $lastName ] );
59
			default:
60
				return $this->translator->trans( 'mail_introduction_generic' );
61
		}
62
	}
63
64
	public function createInformalLastnameGreeting( string $salutation, string $lastName, string $title ): string {
65
		if ( $lastName === '' ) {
66
			return $this->translator->trans( 'mail_introduction_generic' );
67
		}
68
69
		$spacedTitle = self::getSpacedTitle( $title );
70
71
		switch ( $salutation ) {
72
			case self::GREETING_MALE:
73
				return $this->translator->trans( 'mail_introduction_male_lastname_informal', [
74
					'%spacedTitle%' => $spacedTitle,
75
					'%lastName%' => $lastName
76
				] );
77
			case self::GREETING_FEMALE:
78
				return $this->translator->trans( 'mail_introduction_female_lastname_informal', [
79
					'%spacedTitle%' => $spacedTitle,
80
					'%lastName%' => $lastName
81
				] );
82
			case self::GREETING_FAMILY:
83
				return $this->translator->trans( 'mail_introduction_family_informal', [
84
					'%spacedTitle%' => $spacedTitle,
85
					'%lastName%' => $lastName
86
				] );
87
			default:
88
				return $this->translator->trans( 'mail_introduction_generic' );
89
		}
90
	}
91
}