Completed
Push — master ( 88699b...ae29c5 )
by Gabriel
13s
created

getConfirmationPageArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
ccs 7
cts 7
cp 1
cc 1
eloc 7
nc 1
nop 2
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Presentation\Presenters;
6
7
use DateTime;
8
use WMDE\Fundraising\Frontend\App\AccessDeniedException;
9
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Model\Applicant;
10
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Model\Application;
11
use WMDE\Fundraising\Frontend\MembershipContext\UseCases\ShowApplicationConfirmation\ShowApplicationConfirmationPresenter;
12
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\DirectDebitPayment;
13
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PaymentMethod;
14
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PayPalPayment;
15
use WMDE\Fundraising\Frontend\Presentation\TwigTemplate;
16
17
/**
18
 * @licence GNU GPL v2+
19
 * @author Kai Nissen < [email protected] >
20
 */
21
class MembershipApplicationConfirmationHtmlPresenter implements ShowApplicationConfirmationPresenter {
22
23 1
	private $template;
24 1
	private $html = '';
25 1
26
	/**
27 1
	 * @var \Exception|null
28 1
	 */
29 1
	private $exception = null;
30 1
31 1
	public function __construct( TwigTemplate $template ) {
32
		$this->template = $template;
33
	}
34
35
	public function presentConfirmation( Application $application, string $updateToken ): void {
36 1
		$this->html = $this->template->render(
37
			$this->getConfirmationPageArguments(
38 1
				$application,
39 1
				$updateToken
40 1
			)
41
		);
42
	}
43
44 1
	/**
45
	 * @return string
46 1
	 * @throws \Exception
47 1
	 */
48 1
	public function getHtml(): string {
49 1
		if ( $this->exception !== null ) {
50
			throw $this->exception;
51
		}
52
53 1
		return $this->html;
54
	}
55 1
56 1
	private function getConfirmationPageArguments( Application $membershipApplication, string $updateToken ): array {
57 1
		return [
58 1
			'membershipApplication' => $this->getApplicationArguments( $membershipApplication, $updateToken ),
59 1
			'person' => $this->getPersonArguments( $membershipApplication->getApplicant() ),
60 1
			'bankData' => $this->getBankDataArguments( $membershipApplication->getPayment()->getPaymentMethod() ),
61 1
			'payPalData' => $this->getPayPalDataArguments(
62
				$membershipApplication->getPayment()->getPaymentMethod()
63
			)
64
		];
65 1
	}
66 1
67
	private function getApplicationArguments( Application $membershipApplication, string $updateToken ): array {
68 1
		return [
69 1
			'id' => $membershipApplication->getId(),
70 1
			'membershipType' => $membershipApplication->getType(),
71
			'paymentType' => $membershipApplication->getPayment()->getPaymentMethod()->getType(),
72
			'status' => $this->mapStatus( $membershipApplication->isConfirmed() ),
73
			'membershipFee' => $membershipApplication->getPayment()->getAmount()->getEuroString(),
74
			'paymentIntervalInMonths' => $membershipApplication->getPayment()->getIntervalInMonths(),
75
			'updateToken' => $updateToken
76
		];
77
	}
78
79
	private function getPersonArguments( Applicant $applicant ): array {
80
		return [
81
			'salutation' => $applicant->getName()->getSalutation(),
82
			'title' => $applicant->getName()->getTitle(),
83
			'fullName' => $applicant->getName()->getFullName(),
84
			'streetAddress' => $applicant->getPhysicalAddress()->getStreetAddress(),
85
			'postalCode' => $applicant->getPhysicalAddress()->getPostalCode(),
86
			'city' => $applicant->getPhysicalAddress()->getCity(),
87
			'email' => $applicant->getEmailAddress(),
88
		];
89
	}
90
91
	private function getBankDataArguments( PaymentMethod $payment ): array {
92
		if ( $payment instanceof DirectDebitPayment ) {
93
			return [
94
				'iban' => $payment->getBankData()->getIban()->toString(),
95
				'bic' => $payment->getBankData()->getBic(),
96
				'bankName' => $payment->getBankData()->getBankName(),
97
			];
98
		}
99
100
		return [];
101
	}
102
103
	private function getPayPalDataArguments( PaymentMethod $payment ): array {
104
		if ( $payment instanceof PayPalPayment ) {
105
			return [
106
				'firstPaymentDate' => ( new DateTime( $payment->getPayPalData()->getFirstPaymentDate() ) )->format( 'd.m.Y' )
107
			];
108
		}
109
110
		return [];
111
	}
112
113
	/**
114
	 * Maps the membership application's status to a translatable message key
115
	 *
116
	 * @param bool $isConfirmed
117
	 * @return string
118
	 */
119
	private function mapStatus( bool $isConfirmed ): string {
120
		return $isConfirmed ? 'status-booked' : 'status-unconfirmed';
121
	}
122
123
	public function presentApplicationWasAnonymized(): void {
124
		$this->html = 'Membership application was anonymized'; // TODO
125
	}
126
127
	public function presentAccessViolation(): void {
128
		$this->exception = new AccessDeniedException( 'access_denied_membership_confirmation' );
129
	}
130
131
	public function presentTechnicalError( string $message ): void {
132
		$this->html = $message; // TODO
133
	}
134
135
}