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
|
|
|
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
|
|
|
|