Completed
Push — master ( 4b187e...d8fefd )
by Jeroen De
04:05
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\UseCases\ShowMembershipApplicationConfirmation;
6
7
use WMDE\Fundraising\Frontend\Domain\Repositories\GetDonationException;
8
use WMDE\Fundraising\Frontend\Domain\Repositories\MembershipApplicationRepository;
9
use WMDE\Fundraising\Frontend\Infrastructure\MembershipApplicationAuthorizer;
10
use WMDE\Fundraising\Frontend\Infrastructure\MembershipApplicationTokenFetcher;
11
12
/**
13
 * @license GNU GPL v2+
14
 * @author Kai Nissen < [email protected] >
15
 */
16
class ShowMembershipApplicationConfirmationUseCase {
17
18
	private $authorizer;
19
	private $repository;
20
	/**
21
	 * @var MembershipApplicationTokenFetcher
22
	 */
23
	private $tokenFetcher;
24
25
	public function __construct( MembershipApplicationAuthorizer $authorizer, MembershipApplicationRepository $repository,
26
								 MembershipApplicationTokenFetcher $tokenFetcher ) {
27
		$this->authorizer = $authorizer;
28
		$this->repository = $repository;
29
		$this->tokenFetcher = $tokenFetcher;
30
	}
31
32
	public function showConfirmation( ShowMembershipAppConfirmationRequest $request ): ShowMembershipAppConfirmationResponse {
33
		if ( $this->authorizer->canAccessApplication( $request->getApplicationId() ) ) {
34
			$donation = $this->getMembershipApplicationById( $request->getApplicationId() );
35
36
			if ( $donation !== null ) {
37
				return ShowMembershipAppConfirmationResponse::newValidResponse(
38
					$donation,
39
					$this->tokenFetcher->getTokens( $request->getApplicationId() )->getUpdateToken()
40
				);
41
			}
42
		}
43
44
		return ShowMembershipAppConfirmationResponse::newNotAllowedResponse();
45
	}
46
47
	private function getMembershipApplicationById( int $donationId ) {
48
		try {
49
			return $this->repository->getApplicationById( $donationId );
50
		}
51
		catch ( GetDonationException $ex ) {
52
			return null;
53
		}
54
	}
55
56
}