Completed
Push — master ( cdf7ea...6a5bd4 )
by
unknown
02:42 queued 38s
created

AddressChange::newDonationAddressChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Entities;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Ramsey\Uuid\Uuid;
9
10
/**
11
 * @since 8.0
12
 *
13
 * @ORM\Table(
14
 *     name="address_change",
15
 *     indexes={
16
 *     	@ORM\Index(name="ac_export_date", columns={"export_date"}),
17
 *      @ORM\Index(name="ac_ext_id", columns={"external_id_type", "external_id"})
18
 *     }
19
 * )
20
 * @ORM\Entity
21
 */
22
class AddressChange {
23
24
	public const ADDRESS_TYPE_PERSON = 'person';
25
26
	public const ADDRESS_TYPE_COMPANY = 'company';
27
28
	public const EXTERNAL_ID_TYPE_DONATION = 'donation';
29
30
	public const EXTERNAL_ID_TYPE_MEMBERSHIP = 'membership';
31
32
	/**
33
	 * @var integer
34
	 *
35
	 * @ORM\Column(name="id", type="integer")
36
	 * @ORM\Id
37
	 * @ORM\GeneratedValue(strategy="IDENTITY")
38
	 */
39
	private $id;
40
41
	/**
42
	 * @ORM\ManyToOne(targetEntity="Address")
43
	 * @ORM\JoinColumn(name="address_id", referencedColumnName="id")
44
	 */
45
	private $address;
46
47
	/**
48
	 * @var string
49
	 *
50
	 * @ORM\Column(name="current_identifier", type="string", length=36, nullable=true, unique=true)
51
	 */
52
	private $identifier;
53
54
	/**
55
	 * @var string
56
	 *
57
	 * @ORM\Column(name="previous_identifier", type="string", length=36, nullable=true, unique=true)
58
	 */
59
	private $previousIdentifier;
60
61
	/**
62
	 * @var string
63
	 *
64
	 * @ORM\Column(name="address_type", type="string", length = 10)
65
	 */
66
	private $addressType;
67
68
	/**
69
	 * @var int
70
	 *
71
	 * @ORM\Column(name="external_id", type="integer")
72
	 */
73
	private $externalId;
74
75
	/**
76
	 * @var string
77
	 *
78
	 * @ORM\Column(name="external_id_type", type="string", length=10 )
79
	 */
80
	private $externalIdType;
81
82
	/**
83
	 * Date of last export
84
	 *
85
	 * No getter / setter needed -> read access is in AddressChange repo and update in exporter code
86
	 *
87
	 * @var \DateTime
88
	 *
89
	 * @ORM\Column(name="export_date", type="datetime", nullable=true)
90
	 */
91
	private $exportDate;
92
93
	/**
94
	 * When this was created
95
	 *
96
	 * No getter / setter needed, modification is in AddressChange repo
97
	 *
98
	 * @var \DateTime
99
	 * @ORM\Column(name="created_at", type="datetime")
100
	 */
101
	private $createdAt;
102
103
	/**
104
	 * When this was last modified
105
	 *
106
	 * No getter / setter needed, modification is in AddressChange repo
107
	 *
108
	 * @var \DateTime
109
	 * @ORM\Column(name="modified_at", type="datetime")
110
	 */
111
	private $modifiedAt;
112
113
	/**
114
	 * @var boolean
115
	 *
116
	 * @ORM\Column(name="donation_receipt", type="boolean", nullable=false)
117
	 */
118
	private $donationReceipt = true;
119
120
121 2
	private function __construct( string $addressType, string $externalIdType, int $externalId ) {
122 2
		$this->createdAt = new \DateTime();
123 2
		$this->modifiedAt = new \DateTime();
124 2
		$this->addressType = $addressType;
125 2
		$this->externalIdType = $externalIdType;
126 2
		$this->externalId = $externalId;
127 2
		if ($this->identifier === null) {
128 2
			$this->generateUuid();
129
		}
130 2
	}
131
132 1
	public static function newDonationAddressChange( string $addressType, int $donationId ): self {
133 1
		return new self( $addressType, self::EXTERNAL_ID_TYPE_DONATION, $donationId );
134
	}
135
136 1
	public static function newMembershipAddressChange( string $addressType, int $membershipId ): self {
137 1
		return new self( $addressType, self::EXTERNAL_ID_TYPE_MEMBERSHIP, $membershipId );
138
	}
139
140 2
	private function generateUuid(): void {
141 2
		$this->identifier = Uuid::uuid4()->toString();
142 2
	}
143
144 1
	public function updateAddressIdentifier(): void {
145 1
		$this->previousIdentifier = $this->getCurrentIdentifier();
146 1
		$this->generateUuid();
147 1
	}
148
149 2
	public function getCurrentIdentifier(): string {
150 2
		if ($this->identifier === null) {
151
			$this->generateUuid();
152
		}
153 2
		return $this->identifier;
154
	}
155
156 1
	public function getPreviousIdentifier(): ?string {
157 1
		return $this->previousIdentifier;
158
	}
159
160
	public function isOptedIntoDonationReceipt() {
161
		return $this->donationReceipt;
162
	}
163
164
	public function setDonationReceipt( $donationReceipt ) {
165
		$this->donationReceipt = $donationReceipt;
166
	}
167
168
	public function getAddress(): ?Address {
169
		return $this->address;
170
	}
171
172
	public function setAddress( Address $address ) {
173
		$this->address = $address;
174
	}
175
176
	public function getAddressType(): string {
177
		return $this->addressType;
178
	}
179
}
180