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

ApplyForMembershipUseCase::applyForMembership()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\MembershipApplicationContext\UseCases\ApplyForMembership;
6
7
use WMDE\Fundraising\Frontend\Infrastructure\TemplateBasedMailer;
8
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Authorization\MembershipApplicationTokenFetcher;
9
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Domain\Model\MembershipApplication;
10
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Domain\Repositories\MembershipApplicationRepository;
11
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Tracking\MembershipApplicationPiwikTracker;
12
use WMDE\Fundraising\Frontend\MembershipApplicationContext\Tracking\MembershipApplicationTracker;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class ApplyForMembershipUseCase {
19
20
	/* private */ const YEARLY_PAYMENT_MODERATION_THRESHOLD_IN_EURO = 1000;
21
22
	private $repository;
23
	private $tokenFetcher;
24
	private $mailer;
25
	private $validator;
26
	private $piwikTracker;
27
28
	public function __construct( MembershipApplicationRepository $repository,
29
		MembershipApplicationTokenFetcher $tokenFetcher, TemplateBasedMailer $mailer,
30
		MembershipApplicationValidator $validator, MembershipApplicationTracker $tracker,
31
		MembershipApplicationPiwikTracker $piwikTracker ) {
32
33
		$this->repository = $repository;
34
		$this->tokenFetcher = $tokenFetcher;
35
		$this->mailer = $mailer;
36
		$this->validator = $validator;
37
		$this->membershipApplicationTracker = $tracker;
0 ignored issues
show
Bug introduced by
The property membershipApplicationTracker does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
		$this->piwikTracker = $piwikTracker;
39
	}
40
41
	public function applyForMembership( ApplyForMembershipRequest $request ): ApplyForMembershipResponse {
42
		$validationResult = $this->validator->validate( $request );
43
		if ( !$validationResult->isSuccessful() ) {
44
			// TODO: return failures (note that we have infrastructure failures that are not ConstraintViolations)
45
			return ApplyForMembershipResponse::newFailureResponse( $validationResult );
46
		}
47
48
		$application = $this->newApplicationFromRequest( $request );
49
50
		if ( $this->applicationNeedsModeration( $application ) ) {
51
			$application->markForModeration();
52
		}
53
54
		// TODO: handle exceptions
55
		$this->repository->storeApplication( $application );
56
57
		// TODO: handle exceptions
58
		$this->membershipApplicationTracker->trackApplication( $application->getId(), $request->getTrackingInfo() );
59
60
		// TODO: handle exceptions
61
		$this->piwikTracker->trackApplication( $application->getId(), $request->getPiwikTrackingString() );
62
63
		// TODO: handle exceptions
64
		$this->sendConfirmationEmail( $application );
65
66
		// TODO: handle exceptions
67
		$tokens = $this->tokenFetcher->getTokens( $application->getId() );
68
69
		return ApplyForMembershipResponse::newSuccessResponse(
70
			$tokens->getAccessToken(),
71
			$tokens->getUpdateToken(),
72
			$application
73
		);
74
	}
75
76
	private function newApplicationFromRequest( ApplyForMembershipRequest $request ): MembershipApplication {
77
		return ( new MembershipApplicationBuilder() )->newApplicationFromRequest( $request );
78
	}
79
80
	private function sendConfirmationEmail( MembershipApplication $application ) {
81
		$this->mailer->sendMail(
82
			$application->getApplicant()->getEmailAddress(),
83
			[
84
				'membershipType' => $application->getType(),
85
				'membershipFee' => $application->getPayment()->getAmount()->getEuroString(),
86
				'paymentIntervalInMonths' => $application->getPayment()->getIntervalInMonths(),
87
				'salutation' => $application->getApplicant()->getPersonName()->getSalutation(),
88
				'title' => $application->getApplicant()->getPersonName()->getTitle(),
89
				'lastName' => $application->getApplicant()->getPersonName()->getLastName()
90
			]
91
		);
92
	}
93
94
	private function applicationNeedsModeration( MembershipApplication $application ): bool {
95
		return
96
			$application->getPayment()->getYearlyAmount()->getEuroFloat()
97
			> self::YEARLY_PAYMENT_MODERATION_THRESHOLD_IN_EURO;
98
	}
99
100
}
101