Passed
Pull Request — master (#1872)
by Gabriel
61:41
created

AddressType   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 31
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A donorToPresentationAddressType() 0 6 2
A presentationAddressTypeToDomainAddressType() 0 5 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure;
6
7
use WMDE\Fundraising\DonationContext\Domain\Model\Donor;
8
9
/**
10
 * This class is for generating the expected address type strings for the AddDonationRequest und UpdateDonorRequest.
11
 */
12
class AddressType {
13
	public const PERSON = 'person';
14
	public const COMPANY = 'company';
15
	public const EMAIL = 'email';
16
	public const ANONYMOUS = 'anonymous';
17
18
	public const LEGACY_PERSON = 'person';
19
	public const LEGACY_COMPANY = 'firma';
20
	public const LEGACY_EMAIL = 'email';
21
	public const LEGACY_ANONYMOUS = 'anonym';
22
23
	private const PRESENTATION_TO_DOMAIN = [
24
		self::LEGACY_PERSON => self::PERSON,
25
		self::LEGACY_COMPANY => self::COMPANY,
26
		self::LEGACY_EMAIL => self::EMAIL,
27
		self::LEGACY_ANONYMOUS => self::ANONYMOUS
28
	];
29
30
	public static function presentationAddressTypeToDomainAddressType( string $presentationAddressType ): string {
31
		if ( !isset( self::PRESENTATION_TO_DOMAIN[$presentationAddressType] ) ) {
32
			throw new \UnexpectedValueException( sprintf( 'Unexpected Presentation Address Type: %s', $presentationAddressType, ) );
33
		}
34
		return self::PRESENTATION_TO_DOMAIN[$presentationAddressType];
35
	}
36
37
	public static function donorToPresentationAddressType( Donor $donor ): string {
38
		$invertedMap = array_flip( self::PRESENTATION_TO_DOMAIN );
39
		if ( !isset( $invertedMap[$donor->getDonorType()] ) ) {
40
			throw new \UnexpectedValueException( sprintf( 'Unexpected Donor Type: %s', $donor->getDonorType(), ) );
41
		}
42
		return $invertedMap[$donor->getDonorType()];
43
	}
44
45
}
46