Completed
Pull Request — master (#2)
by Gabriel
127:24 queued 62:20
created

AddressChange::performAddressChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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 $createdAt;
28
29
	private $modifiedAt;
30
31
	private function __construct( string $addressType, ?string $identifier = null, ?Address $address = null,
32
								  ?\DateTime $createdAt = null ) {
33
		$this->addressType = $addressType;
34
		$this->identifier = $identifier;
35
		$this->address = $address;
36
		if ( $identifier === null ) {
37
			$this->generateUuid();
38
		} elseif ( !Uuid::isValid( $identifier ) ) {
39
			throw new \InvalidArgumentException( 'Identifier must be a valid UUID' );
40
		}
41
		$this->createdAt = $createdAt ?? new \DateTime();
42
		$this->modifiedAt = clone( $this->createdAt );
43
	}
44
45
	public static function createNewPersonAddressChange( ?string $identifier = null, ?Address $address = null,
46
														 ?\DateTime $createdAt = null ): self {
47
		return new AddressChange( self::ADDRESS_TYPE_PERSON, $identifier, $address, $createdAt );
48
	}
49
50
	public static function createNewCompanyAddressChange( ?string $identifier = null, ?Address $address = null,
51
														  ?\DateTime $createdAt = null ): self {
52
		return new AddressChange( self::ADDRESS_TYPE_COMPANY, $identifier, $address, $createdAt );
53
	}
54
55
	private function generateUuid(): void {
56
		$this->identifier = Uuid::uuid4()->toString();
57
	}
58
59
	public function performAddressChange( Address $address ): void {
60
		if ( $this->address !== null ) {
61
			throw new LogicException( 'Cannot perform address change for instances that already have an address.' );
62
		}
63
		$this->address = $address;
64
		$this->previousIdentifier = $this->getCurrentIdentifier();
65
		$this->modifiedAt = new \DateTime();
66
		$this->generateUuid();
67
		$this->resetExportState();
68
	}
69
70
	public function getCurrentIdentifier(): string {
71
		if ( $this->identifier === null ) {
72
			$this->generateUuid();
73
		}
74
		return $this->identifier;
75
	}
76
77
	public function getPreviousIdentifier(): ?string {
78
		return $this->previousIdentifier;
79
	}
80
81
	public function getAddress(): ?Address {
82
		return $this->address;
83
	}
84
85
	public function isPersonalAddress(): bool {
86
		return $this->addressType === self::ADDRESS_TYPE_PERSON;
87
	}
88
89
	public function isCompanyAddress(): bool {
90
		return $this->addressType === self::ADDRESS_TYPE_COMPANY;
91
	}
92
93
	public function markAsExported(): void {
94
		if ( $this->isExported() ) {
95
			throw new LogicException( 'Address changes can only be exported once.' );
96
		}
97
		$this->exportDate = new \DateTime();
98
	}
99
100
	private function resetExportState(): void {
101
		$this->exportDate = null;
102
	}
103
104
	public function isExported(): bool {
105
		return $this->exportDate !== null;
106
	}
107
108
	public function isModified(): bool {
109
		return $this->createdAt < $this->modifiedAt;
110
	}
111
}
112