|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\AddressChangeContext\Domain\Model; |
|
6
|
|
|
|
|
7
|
|
|
use WMDE\Fundraising\AddressChangeContext\Infrastructure\RandomUuidGenerator; |
|
8
|
|
|
|
|
9
|
|
|
class AddressChangeBuilder { |
|
10
|
|
|
|
|
11
|
|
|
private static ?UuidGenerator $uuidGenerator = null; |
|
12
|
|
|
|
|
13
|
|
|
private ?AddressType $addressType; |
|
14
|
|
|
private ?string $referenceType; |
|
15
|
|
|
private ?int $referenceId; |
|
16
|
|
|
|
|
17
|
10 |
|
private function __construct( |
|
18
|
|
|
private ?AddressChangeId $identifier = null, |
|
19
|
|
|
private ?Address $address = null, |
|
20
|
|
|
private ?\DateTime $createdAt = null |
|
21
|
|
|
) { |
|
22
|
10 |
|
$this->addressType = null; |
|
23
|
10 |
|
$this->referenceType = null; |
|
24
|
10 |
|
$this->referenceId = null; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
10 |
|
public static function create( ?AddressChangeId $identifier = null, ?Address $address = null, ?\DateTime $createdAt = null ): self { |
|
28
|
10 |
|
if ( $identifier === null ) { |
|
29
|
9 |
|
$identifier = AddressChangeId::fromString( self::generateUuid() ); |
|
30
|
|
|
} |
|
31
|
10 |
|
return new self( $identifier, $address, $createdAt ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
6 |
|
public function forPerson(): self { |
|
35
|
6 |
|
return $this->setAddressType( AddressType::Person ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
public function forCompany(): self { |
|
39
|
3 |
|
return $this->setAddressType( AddressType::Company ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
7 |
|
public function setAddressType( AddressType $addressType ): self { |
|
43
|
7 |
|
if ( $this->addressType !== null ) { |
|
44
|
2 |
|
throw new \RuntimeException( 'You can only specify address type once' ); |
|
45
|
|
|
} |
|
46
|
7 |
|
$this->addressType = $addressType; |
|
47
|
7 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
public function forDonation( int $donationId ): self { |
|
51
|
6 |
|
return $this->setReference( $donationId, AddressChange::EXTERNAL_ID_TYPE_DONATION ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
public function forMembership( int $membershipId ): self { |
|
55
|
3 |
|
return $this->setReference( $membershipId, AddressChange::EXTERNAL_ID_TYPE_MEMBERSHIP ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
7 |
|
private function setReference( int $referenceId, string $referenceType ): self { |
|
59
|
7 |
|
if ( $this->referenceType !== null ) { |
|
60
|
2 |
|
throw new \RuntimeException( 'You can only specify reference type once' ); |
|
61
|
|
|
} |
|
62
|
7 |
|
$this->referenceType = $referenceType; |
|
63
|
7 |
|
$this->referenceId = $referenceId; |
|
64
|
7 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
6 |
|
public function build(): AddressChange { |
|
68
|
6 |
|
if ( $this->referenceType === null || $this->addressType === null || $this->referenceId === null || $this->identifier === null ) { |
|
69
|
1 |
|
throw new \RuntimeException( 'You must specify address type and reference' ); |
|
70
|
|
|
} |
|
71
|
5 |
|
return new AddressChange( $this->addressType, $this->referenceType, $this->referenceId, $this->identifier, $this->address, $this->createdAt ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public static function setUuidGenerator( UuidGenerator $generator ): void { |
|
75
|
1 |
|
self::$uuidGenerator = $generator; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
9 |
|
public static function generateUuid(): string { |
|
79
|
9 |
|
if ( self::$uuidGenerator === null ) { |
|
80
|
|
|
self::$uuidGenerator = new RandomUuidGenerator(); |
|
81
|
|
|
} |
|
82
|
9 |
|
return call_user_func( [ self::$uuidGenerator, 'generate' ] ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|