Passed
Pull Request — master (#147)
by
unknown
09:57
created

AddressChange::getPreviousIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

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
eloc 2
nc 1
nop 0
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( name="address_change" )
14
 * @ORM\Entity
15
 */
16
class AddressChange {
17
18
	/**
19
	 * @var integer
20
	 *
21
	 * @ORM\Column(name="id", type="integer")
22
	 * @ORM\Id
23
	 * @ORM\GeneratedValue(strategy="IDENTITY")
24
	 */
25
	private $id;
26
27
	/**
28
	 * @ORM\ManyToOne(targetEntity="Address")
29
	 * @ORM\JoinColumn(name="address_id", referencedColumnName="id")
30
	 */
31
	private $address;
32
33
	/**
34
	 * @var string
35
	 *
36
	 * @ORM\Column(name="current_identifier", type="string", length=36, nullable=true)
37
	 */
38
	private $identifier;
39
40
	/**
41
	 * @var string
42
	 *
43
	 * @ORM\Column(name="previous_identifier", type="string", length=36, nullable=true)
44
	 */
45
	private $previousIdentifier;
46
47
	/**
48
	 * Ensure that every AddressChange object gets a UUID but do not overwrite pre-fetched Doctrine values
49
	 */
50 2
	public function __construct() {
51 2
		if ($this->identifier === null) {
52 2
			$this->generateUuid();
53
		}
54 2
	}
55
56 2
	private function generateUuid(): void {
57 2
		$this->identifier = Uuid::uuid4()->toString();
58 2
	}
59
60 1
	public function updateAddressIdentifier(): void {
61 1
		$this->previousIdentifier = $this->getCurrentIdentifier();
62 1
		$this->generateUuid();
63 1
	}
64
65 2
	public function getCurrentIdentifier(): string {
66 2
		if ($this->identifier === null) {
67
			$this->generateUuid();
68
		}
69 2
		return $this->identifier;
70
	}
71
72 1
	public function getPreviousIdentifier(): ?string {
73 1
		return $this->previousIdentifier;
74
	}
75
76
	public function getAddress(): Address {
77
		return $this->address;
78
	}
79
80
	public function setAddress( Address $address ) {
81
		$this->address = $address;
82
	}
83
}
84