AddressType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 34
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 2 1
A getName() 0 2 1
A convertToPHPValue() 0 9 2
A convertToDatabaseValue() 0 7 2
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