Completed
Pull Request — master (#1)
by Tim
79:06 queued 13:58
created

AddressChange   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreviousIdentifier() 0 2 1
A isPersonalAddress() 0 2 1
A createNewPersonAddressChange() 0 2 1
A createNewCompanyAddressChange() 0 2 1
A markAsExported() 0 5 2
A isExported() 0 2 1
A generateUuid() 0 2 1
A getAddress() 0 2 1
A getCurrentIdentifier() 0 5 2
A resetExportState() 0 2 1
A isCompanyAddress() 0 2 1
A performAddressChange() 0 8 2
A __construct() 0 8 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\AddressChange\Domain\Model;
6
7
use LogicException;
8
use Ramsey\Uuid\Uuid;
9
10
class AddressChange {
11
12
	public const ADDRESS_TYPE_PERSON = 'person';
13
	public const ADDRESS_TYPE_COMPANY = 'company';
14
15
	private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
16
17
	private $identifier;
18
19
	private $previousIdentifier;
20
21
	private $address;
22
23
	private $addressType;
24
25
	private $exportDate;
26
27
	private function __construct( string $addressType, ?string $identifier = null, ?Address $address = null ) {
28
		$this->addressType = $addressType;
29
		$this->identifier = $identifier;
30
		$this->address = $address;
31
		if ( $identifier === null ) {
32
			$this->generateUuid();
33
		} elseif ( !Uuid::isValid( $identifier ) ) {
34
			throw new \InvalidArgumentException( 'Identifier must be a valid UUID' );
35
		}
36
	}
37
38
	public static function createNewPersonAddressChange( ?string $identifier = null, ?Address $address = null ): self {
39
		return new AddressChange( self::ADDRESS_TYPE_PERSON, $identifier, $address );
40
	}
41
42
	public static function createNewCompanyAddressChange( ?string $identifier = null, ?Address $address = null ): self {
43
		return new AddressChange( self::ADDRESS_TYPE_COMPANY, $identifier, $address );
44
	}
45
46
	private function generateUuid(): void {
47
		$this->identifier = Uuid::uuid4()->toString();
48
	}
49
50
	public function performAddressChange( Address $address ): void {
51
		if ( $this->address !== null ) {
52
			throw new LogicException( 'Cannot perform address change for instances that already have an address.' );
53
		}
54
		$this->address = $address;
55
		$this->previousIdentifier = $this->getCurrentIdentifier();
56
		$this->generateUuid();
57
		$this->resetExportState();
58
	}
59
60
	public function getCurrentIdentifier(): string {
61
		if ( $this->identifier === null ) {
62
			$this->generateUuid();
63
		}
64
		return $this->identifier;
65
	}
66
67
	public function getPreviousIdentifier(): ?string {
68
		return $this->previousIdentifier;
69
	}
70
71
	public function getAddress(): ?Address {
72
		return $this->address;
73
	}
74
75
	public function isPersonalAddress(): bool {
76
		return $this->addressType === self::ADDRESS_TYPE_PERSON;
77
	}
78
79
	public function isCompanyAddress(): bool {
80
		return $this->addressType === self::ADDRESS_TYPE_COMPANY;
81
	}
82
83
	public function markAsExported(): void {
84
		if ( $this->isExported() ) {
85
			throw new LogicException( 'Address changes can only be exported once.' );
86
		}
87
		$this->exportDate = new \DateTime();
88
	}
89
90
	private function resetExportState(): void {
91
		$this->exportDate = null;
92
	}
93
94
	public function isExported(): bool {
95
		return $this->exportDate !== null;
96
	}
97
}
98