Completed
Pull Request — master (#650)
by Jeroen De
19:07 queued 01:42
created

cancelApplication()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\MembershipContext\UseCases\CancelMembershipApplication;
6
7
use WMDE\Fundraising\Frontend\Infrastructure\TemplateBasedMailer;
8
use WMDE\Fundraising\Frontend\MembershipContext\Authorization\ApplicationAuthorizer;
9
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Model\Application;
10
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Repositories\ApplicationRepository;
11
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Repositories\GetMembershipApplicationException;
12
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Repositories\StoreMembershipApplicationException;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class CancelMembershipApplicationUseCase {
19
20
	private $authorizer;
21
	private $repository;
22
	private $mailer;
23
24
	public function __construct( ApplicationAuthorizer $authorizer,
25
		ApplicationRepository $repository, TemplateBasedMailer $mailer ) {
26
27
		$this->authorizer = $authorizer;
28
		$this->repository = $repository;
29
		$this->mailer = $mailer;
30
	}
31
32
	public function cancelApplication( CancellationRequest $request ): CancellationResponse {
33
		if ( !$this->authorizer->canModifyApplication( $request->getApplicationId() ) ) {
34
			return $this->newFailureResponse( $request );
35
		}
36
37
		$application = $this->getApplicationById( $request->getApplicationId() );
38
39
		if ( $application === null ) {
40
			return $this->newFailureResponse( $request );
41
		}
42
43
		$application->cancel();
44
45
		try {
46
			$this->repository->storeApplication( $application );
47
		}
48
		catch ( StoreMembershipApplicationException $ex ) {
49
			// TODO: log?
50
			return $this->newFailureResponse( $request );
51
		}
52
53
		$this->sendConfirmationEmail( $application );
54
55
		return $this->newSuccessResponse( $request );
56
	}
57
58
	private function getApplicationById( int $id ) {
59
		try {
60
			return $this->repository->getApplicationById( $id );
61
		}
62
		catch ( GetMembershipApplicationException $ex ) {
63
			// TODO: log?
64
			return null;
65
		}
66
	}
67
68
	private function newFailureResponse( CancellationRequest $request ) {
69
		return new CancellationResponse( $request->getApplicationId(), CancellationResponse::IS_FAILURE );
70
	}
71
72
	private function newSuccessResponse( CancellationRequest $request ) {
73
		return new CancellationResponse( $request->getApplicationId(), CancellationResponse::IS_SUCCESS );
74
	}
75
76
	private function sendConfirmationEmail( Application $application ) {
77
		$this->mailer->sendMail(
78
			$application->getApplicant()->getEmailAddress(),
79
			$this->getConfirmationMailTemplateArguments( $application )
80
		);
81
	}
82
83
	private function getConfirmationMailTemplateArguments( Application $application ): array {
84
		return [
85
			'applicationId' => $application->getId(),
86
			'membershipApplicant' => [
87
				'salutation' => $application->getApplicant()->getName()->getSalutation(),
88
				'title' => $application->getApplicant()->getName()->getTitle(),
89
				'lastName' => $application->getApplicant()->getName()->getLastName()
90
			]
91
		];
92
	}
93
94
}
95