Completed
Pull Request — master (#923)
by Jeroen De
61:01
created

SofortNotificationHandler::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\App\RouteHandlers;
6
7
use DateTime;
8
use Psr\Log\LogLevel;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use WMDE\Fundraising\Frontend\DonationContext\UseCases\SofortPaymentNotification\SofortPaymentNotificationUseCase;
12
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
13
use WMDE\Fundraising\Frontend\PaymentContext\RequestModel\SofortNotificationRequest;
14
use WMDE\Fundraising\Frontend\PaymentContext\ResponseModel\SofortNotificationResponse;
15
16
class SofortNotificationHandler {
17
18
	private $ffFactory;
19
20
	/**
21
	 * @var Request
22
	 */
23
	private $request;
24
25
	public function __construct( FunFunFactory $ffFactory ) {
26
		$this->ffFactory = $ffFactory;
27
	}
28
29
	public function handle( Request $request ): Response {
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
30
		$this->request = $request;
31
32
		$useCaseRequest = $this->newUseCaseRequest();
33
34
		if ( $useCaseRequest === null ) {
35
			$this->logWebRequest( [ 'message' => 'Bad notification time' ], LogLevel::ERROR );
36
			return new Response( 'Bad request', Response::HTTP_BAD_REQUEST );
37
		}
38
39
		$response = $this->newUseCase()->handleNotification( $useCaseRequest );
40
41
		$this->logResponseIfNeeded( $response );
42
43
		if ( $response->hasErrors() ) {
44
			return new Response( 'Error', Response::HTTP_INTERNAL_SERVER_ERROR );
45
		}
46
47
		if ( $response->notificationWasHandled() ) {
48
			return new Response( 'Ok', Response::HTTP_OK );
49
		}
50
51
		return new Response( 'Bad request', Response::HTTP_BAD_REQUEST );
52
	}
53
54
	private function newUseCase(): SofortPaymentNotificationUseCase {
55
		return $this->ffFactory->newHandleSofortPaymentNotificationUseCase( $this->request->query->get( 'updateToken' ) );
56
	}
57
58
	private function newUseCaseRequest(): ?SofortNotificationRequest {
59
		$time = $this->getTimeFromRequest();
60
61
		if ( $time === false ) {
62
			return null;
63
		}
64
65
		$useCaseRequest = new SofortNotificationRequest();
66
67
		$useCaseRequest->setTime( $time );
68
		$useCaseRequest->setDonationId( $this->request->query->getInt( 'id' ) );
69
		$useCaseRequest->setTransactionId( $this->request->request->get( 'transaction', '' ) );
70
71
		return $useCaseRequest;
72
	}
73
74
	private function getTimeFromRequest() {
75
		return DateTime::createFromFormat( DateTime::ATOM, $this->request->request->get( 'time', '' ) );
76
	}
77
78
	private function logResponseIfNeeded( SofortNotificationResponse $response ) {
79
		if ( $response->notificationWasHandled() ) {
80
			return;
81
		}
82
83
		$this->logWebRequest(
84
			$response->getContext(),
85
			$response->hasErrors() ? LogLevel::ERROR : LogLevel::INFO
86
		);
87
	}
88
89
	private function logWebRequest( array $context, string $logLevel ) {
90
		$message = $context['message'] ?? 'Sofort request not handled';
91
		unset( $context['message'] );
92
93
		$context['post_vars'] = $this->request->request->all();
94
		$context['query_vars'] = $this->request->query->all();
95
		$this->ffFactory->getSofortLogger()->log( $logLevel, $message, $context );
96
	}
97
98
}
99