Completed
Pull Request — master (#976)
by wiese
61:34
created

Application::statusAllowsForBooking()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\MembershipContext\Domain\Model;
6
7
use RuntimeException;
8
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PaymentType;
9
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PayPalPayment;
10
11
/**
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class Application {
16
17
	public const ACTIVE_MEMBERSHIP = 'active';
18
	public const SUSTAINING_MEMBERSHIP = 'sustaining';
19
20
	private const NO_MODERATION_NEEDED = false;
21
	private const NEEDS_MODERATION = true;
22
23
	private const IS_CURRENT = false;
24
	private const IS_CANCELLED = true;
25
26
	private const IS_CONFIRMED = true;
27
	private const IS_PENDING = false;
28
29
	const IS_DELETED = true;
30
	const IS_NOT_DELETED = false;
31
32
	/**
33
	 * @var int|null
34
	 */
35
	private $id;
36
37
	private $type;
38
	private $applicant;
39
	private $payment;
40
	private $needsModeration;
41
	private $isCancelled;
42
	private $isConfirmed;
43
	private $isDeleted;
44
	private $donationReceipt;
45
46
	public static function newApplication( string $type, Applicant $applicant, Payment $payment, ?bool $donationReceipt ): self {
47
		return new self(
48
			null,
49
			$type,
50
			$applicant,
51
			$payment,
52
			self::NO_MODERATION_NEEDED,
53
			self::IS_CURRENT,
54
			self::IS_PENDING,
55
			self::IS_NOT_DELETED,
56
			$donationReceipt
57
		);
58
	}
59
60
	public function __construct( ?int $id, string $type, Applicant $applicant, Payment $payment,
61
		bool $needsModeration, bool $isCancelled, bool $isConfirmed, bool $isDeleted, ?bool $donationReceipt ) {
62
		$this->id = $id;
63
		$this->type = $type;
64
		$this->applicant = $applicant;
65
		$this->payment = $payment;
66
		$this->needsModeration = $needsModeration;
67
		$this->isCancelled = $isCancelled;
68
		$this->isConfirmed = $isConfirmed;
69
		$this->isDeleted = $isDeleted;
70
		$this->donationReceipt = $donationReceipt;
71
	}
72
73
	public function getId(): ?int {
74
		return $this->id;
75
	}
76
77
	public function hasId(): bool {
78
		return $this->id !== null;
79
	}
80
81
	public function getApplicant(): Applicant {
82
		return $this->applicant;
83
	}
84
85
	public function getPayment(): Payment {
86
		return $this->payment;
87
	}
88
89
	public function getType(): string {
90
		return $this->type;
91
	}
92
93
	public function getDonationReceipt(): ?bool {
94
		return $this->donationReceipt;
95
	}
96
97
	/**
98
	 * @param int $id
99
	 * @throws \RuntimeException
100
	 */
101
	public function assignId( int $id ): void {
102
		if ( $this->id !== null && $this->id !== $id ) {
103
			throw new \RuntimeException( 'Id cannot be changed after initial assignment' );
104
		}
105
106
		$this->id = $id;
107
	}
108
109
	public function cancel(): void {
110
		$this->isCancelled = self::IS_CANCELLED;
111
	}
112
113
	public function markForModeration(): void {
114
		$this->needsModeration = self::NEEDS_MODERATION;
115
	}
116
117
	public function isCancelled(): bool {
118
		return $this->isCancelled === self::IS_CANCELLED;
119
	}
120
121
	public function needsModeration(): bool {
122
		return $this->needsModeration === self::NEEDS_MODERATION;
123
	}
124
125
	public function isConfirmed(): bool {
126
		return $this->isConfirmed === self::IS_CONFIRMED;
127
	}
128
129
	public function confirm(): void {
130
		$this->isConfirmed = self::IS_CONFIRMED;
131
	}
132
133
	public function confirmSubscriptionCreated(): void {
134
		if ( !$this->hasExternalPayment() ) {
135
			throw new RuntimeException( 'Only external payments can be confirmed as booked' );
136
		}
137
138
		if ( !$this->statusAllowsForBooking() ) {
139
			throw new RuntimeException( 'Only unconfirmed membership applications can be confirmed as booked' );
140
		}
141
142
		$this->confirm();
143
	}
144
145
	public function hasExternalPayment(): bool {
146
		return $this->getPayment()->getPaymentMethod()->getType() === PaymentType::PAYPAL;
147
	}
148
149
	private function statusAllowsForBooking(): bool {
150
		return !$this->isConfirmed() || $this->needsModeration() || $this->isCancelled();
151
	}
152
153
	public function markAsDeleted(): void {
154
		$this->isDeleted = self::IS_DELETED;
155
	}
156
157
	public function isDeleted(): bool {
158
		return $this->isDeleted;
159
	}
160
161
	public function notifyOfFirstPaymentDate( string $firstPaymentDate ): void {
162
		$paymentMethod = $this->getPayment()->getPaymentMethod();
163
164
		if ( $paymentMethod instanceof PayPalPayment ) {
165
			$paymentMethod->getPayPalData()->setFirstPaymentDate( $firstPaymentDate );
166
		}
167
	}
168
169
}
170