AddressType::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace WMDE\Fundraising\AddressChangeContext\DataAccess\DoctrineTypes;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types\Type;
7
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressType as DomainAddressType;
8
9
class AddressType extends Type {
10
	public function getSQLDeclaration( array $column, AbstractPlatform $platform ): string {
11
		return 'VARCHAR(10)';
12
	}
13
14
	public function convertToPHPValue( mixed $value, AbstractPlatform $platform ): DomainAddressType {
15
		if ( !is_string( $value ) ) {
16
			throw new \LogicException( 'Value from database has to be of type string' );
17
		}
18
		return match ( $value ) {
19
			'person' => DomainAddressType::Person,
20
			'company' => DomainAddressType::Company,
21
			default => throw new \InvalidArgumentException(
22
				"Could not convert address type string ({$value}) to enum"
23
			),
24
		};
25
	}
26
27
	public function convertToDatabaseValue( mixed $value, AbstractPlatform $platform ): string {
28
		if ( !( $value instanceof DomainAddressType ) ) {
29
			throw new \LogicException( "Value from database has to be of type " . DomainAddressType::class );
30
		}
31
		return match ( $value ) {
32
			DomainAddressType::Person => 'person',
33
			DomainAddressType::Company => 'company',
34
		};
35
	}
36
37
	/**
38
	 * @codeCoverageIgnore
39
	 * @return string
40
	 */
41
	public function getName(): string {
42
		return 'AddressType';
43
	}
44
45
}
46