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; |
|
|
|
|
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
|
|
|
|
32
|
|
|
if ( $identifier === null ) { |
33
|
|
|
$this->generateUuid(); |
34
|
|
|
} elseif ( !Uuid::isValid( $identifier ) ) { |
35
|
|
|
throw new \InvalidArgumentException( 'Identifier must be a valid UUID' ); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function createNewPersonAddressChange( ?string $identifier = null, ?Address $address = null ): self { |
40
|
|
|
return new AddressChange( self::ADDRESS_TYPE_PERSON, $identifier, $address ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function createNewCompanyAddressChange( ?string $identifier = null, ?Address $address = null ): self { |
44
|
|
|
return new AddressChange( self::ADDRESS_TYPE_COMPANY, $identifier, $address ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function generateUuid(): void { |
48
|
|
|
$this->identifier = Uuid::uuid4()->toString(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function performAddressChange( Address $address ): void { |
52
|
|
|
if ( $this->address !== null ) { |
53
|
|
|
throw new LogicException( 'Cannot perform address change for instances that already have an address.' ); |
54
|
|
|
} |
55
|
|
|
$this->address = $address; |
56
|
|
|
$this->previousIdentifier = $this->getCurrentIdentifier(); |
57
|
|
|
$this->generateUuid(); |
58
|
|
|
$this->resetExportState(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getCurrentIdentifier(): string { |
62
|
|
|
if ( $this->identifier === null ) { |
63
|
|
|
$this->generateUuid(); |
64
|
|
|
} |
65
|
|
|
return $this->identifier; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getPreviousIdentifier(): ?string { |
69
|
|
|
return $this->previousIdentifier; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getAddress(): ?Address { |
73
|
|
|
return $this->address; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function isPersonalAddress(): bool { |
77
|
|
|
return $this->addressType === self::ADDRESS_TYPE_PERSON; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isCompanyAddress(): bool { |
81
|
|
|
return $this->addressType === self::ADDRESS_TYPE_COMPANY; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function markAsExported(): void { |
85
|
|
|
if ( $this->isExported() ) { |
86
|
|
|
throw new LogicException( 'Address changes can only be exported once.' ); |
87
|
|
|
} |
88
|
|
|
$this->exportDate = new \DateTime(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function resetExportState(): void { |
92
|
|
|
$this->exportDate = null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function isExported(): bool { |
96
|
|
|
return $this->exportDate !== null; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|