Passed
Push — master ( 489f2e...0cdf36 )
by
unknown
289:17 queued 286:28
created

CreditCardPaymentNotificationController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 9 2
A requestIsForPaymentCompletion() 0 2 1
A handleInvalidNotificationType() 0 13 1
A handleBillingNotification() 0 28 4
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\App\Controllers\Payment;
6
7
use Symfony\Component\HttpFoundation\ParameterBag;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use WMDE\Euro\Euro;
11
use WMDE\Fundraising\DonationContext\UseCases\CreditCardPaymentNotification\CreditCardNotificationResponse;
12
use WMDE\Fundraising\DonationContext\UseCases\CreditCardPaymentNotification\CreditCardPaymentNotificationRequest;
13
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
14
15
/**
16
 * @license GPL-2.0-or-later
17
 */
18
class CreditCardPaymentNotificationController {
19
20
	private const MSG_NOT_HANDLED = 'Credit card request "%s" not handled';
21
22
	public function index( FunFunFactory $ffFactory, Request $request ): Response {
23
		$queryParams = $request->query;
24
		$donationId = $queryParams->get( 'donation_id', '' );
25
26
		if ( !$this->requestIsForPaymentCompletion( $queryParams ) ) {
27
			return $this->handleInvalidNotificationType( $ffFactory, $queryParams, $donationId );
28
		}
29
30
		return $this->handleBillingNotification( $ffFactory, $queryParams, $donationId, $request->getClientIp() );
0 ignored issues
show
Bug introduced by
It seems like $request->getClientIp() can also be of type null; however, parameter $clientIp of WMDE\Fundraising\Fronten...leBillingNotification() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
		return $this->handleBillingNotification( $ffFactory, $queryParams, $donationId, /** @scrutinizer ignore-type */ $request->getClientIp() );
Loading history...
31
	}
32
33
	private function requestIsForPaymentCompletion( ParameterBag $query ): bool {
34
		return $query->get( 'function', '' ) === 'billing';
35
	}
36
37
	private function handleInvalidNotificationType( FunFunFactory $ffFactory, ParameterBag $queryParams, string $donationId ): Response {
38
		$ffFactory->getCreditCardLogger()->info(
39
			sprintf( self::MSG_NOT_HANDLED, $queryParams->get( 'function' ) ),
40
			$queryParams->all()
41
		);
42
43
		return new Response( $ffFactory->newCreditCardNotificationPresenter()->present(
44
			new CreditCardNotificationResponse(
45
				false,
46
				sprintf( 'Function "%s" not supported by this end point', $queryParams->get( 'function' ) )
47
			),
48
			$donationId,
49
			$queryParams->get( 'token', '' )
50
		) );
51
	}
52
53
	private function handleBillingNotification( FunFunFactory $ffFactory, ParameterBag $queryParams, string $donationId, string $clientIp ): Response {
54
		$response = $ffFactory->newCreditCardNotificationUseCase( $queryParams->get( 'utoken', '' ) )
55
			->handleNotification(
56
				( new CreditCardPaymentNotificationRequest() )
57
					->setTransactionId( $queryParams->get( 'transactionId', '' ) )
58
					->setDonationId( (int)$donationId )
59
					->setAmount( Euro::newFromCents( (int)$queryParams->get( 'amount' ) ) )
60
					->setCustomerId( $queryParams->get( 'customerId', '' ) )
61
					->setSessionId( $queryParams->get( 'sessionId', '' ) )
62
					->setAuthId( $queryParams->get( 'auth', '' ) )
63
					->setTitle( $queryParams->get( 'title', '' ) )
64
					->setCountry( $queryParams->get( 'country', '' ) )
65
					->setCurrency( $queryParams->get( 'currency', '' ) )
66
			);
67
68
		$loggingContext = $response->getLowLevelError() === null ? [] : [ 'exception' => $response->getLowLevelError() ];
69
		if ( !$response->isSuccessful() ) {
70
			$loggingContext['queryParams'] = $queryParams->all();
71
			$loggingContext['clientIP'] = $clientIp;
72
			$ffFactory->getLogger()->error( 'Credit Card Notification Error: ' . $response->getErrorMessage(), $loggingContext );
73
		} elseif ( $loggingContext !== [] ) {
74
			$ffFactory->getLogger()->warning( 'Failed to send conformation email for credit card notification', $loggingContext );
75
		}
76
77
		return new Response( $ffFactory->newCreditCardNotificationPresenter()->present(
78
			$response,
79
			$donationId,
80
			$queryParams->get( 'token', '' )
81
		) );
82
	}
83
84
}
85