Completed
Pull Request — master (#594)
by Jeroen De
12:09
created

CancelMembershipApplicationUseCase   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B cancelApplication() 0 25 4
A getApplicationById() 0 9 2
A newFailureResponse() 0 3 1
A newSuccessResponse() 0 3 1
A sendConfirmationEmail() 0 6 1
A getConfirmationMailTemplateArguments() 0 10 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\MembershipApplicationContext\UseCases\CancelMembershipApplication;
6
7
use WMDE\Fundraising\Frontend\Infrastructure\TemplateBasedMailer;
8
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Authorization\MembershipApplicationAuthorizer;
9
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Domain\Model\MembershipApplication;
10
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Domain\Repositories\GetMembershipApplicationException;
11
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Domain\Repositories\MembershipApplicationRepository;
12
use WMDE\Fundraising\Frontend\MembershipApplicationContext\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( MembershipApplicationAuthorizer $authorizer,
25
		MembershipApplicationRepository $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( MembershipApplication $application ) {
77
		$this->mailer->sendMail(
78
			$application->getApplicant()->getEmailAddress(),
79
			$this->getConfirmationMailTemplateArguments( $application )
80
		);
81
	}
82
83
	private function getConfirmationMailTemplateArguments( MembershipApplication $application ): array {
84
		return [
85
			'applicationId' => $application->getId(),
86
			'membershipApplicant' => [
87
				'salutation' => $application->getApplicant()->getPersonName()->getSalutation(),
88
				'title' => $application->getApplicant()->getPersonName()->getTitle(),
89
				'lastName' => $application->getApplicant()->getPersonName()->getLastName()
90
			]
91
		];
92
	}
93
94
}
95