|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\App\Controllers\Validation; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use WMDE\Euro\Euro; |
|
12
|
|
|
use WMDE\Fundraising\MembershipContext\UseCases\ValidateMembershipFee\ValidateFeeRequest; |
|
13
|
|
|
use WMDE\Fundraising\MembershipContext\UseCases\ValidateMembershipFee\ValidateFeeResult; |
|
14
|
|
|
use WMDE\Fundraising\MembershipContext\UseCases\ValidateMembershipFee\ValidateMembershipFeeUseCase; |
|
15
|
|
|
|
|
16
|
|
|
class ValidateFeeController { |
|
17
|
|
|
|
|
18
|
|
|
private const ERROR_RESPONSE_MAP = [ |
|
19
|
|
|
ValidateFeeResult::ERROR_TOO_LOW => 'too-low', |
|
20
|
|
|
ValidateFeeResult::ERROR_INTERVAL_INVALID => 'interval-invalid', |
|
21
|
|
|
'not-money' => 'not-money' |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
public function index( Request $httpRequest ): Response { |
|
25
|
|
|
try { |
|
26
|
|
|
$fee = $this->euroFromRequest( $httpRequest ); |
|
27
|
|
|
} |
|
28
|
|
|
catch ( InvalidArgumentException $ex ) { |
|
29
|
|
|
return $this->newJsonErrorResponse( 'not-money' ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$request = ValidateFeeRequest::newInstance() |
|
33
|
|
|
->withFee( $fee ) |
|
34
|
|
|
->withInterval( (int)$httpRequest->request->get( 'paymentIntervalInMonths', '0' ) ) |
|
35
|
|
|
->withApplicantType( $httpRequest->request->get( 'addressType', '' ) ); |
|
36
|
|
|
|
|
37
|
|
|
$response = ( new ValidateMembershipFeeUseCase() )->validate( $request ); |
|
38
|
|
|
|
|
39
|
|
|
if ( $response->isSuccessful() ) { |
|
40
|
|
|
return new JsonResponse( [ 'status' => 'OK' ] ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->newJsonErrorResponse( $response->getErrorCode() ); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function euroFromRequest( Request $httpRequest ): Euro { |
|
47
|
|
|
$currentFeeString = $httpRequest->request->get( 'membershipFee', '' ); |
|
48
|
|
|
if ( !ctype_digit( $currentFeeString ) ) { |
|
49
|
|
|
throw new InvalidArgumentException(); |
|
50
|
|
|
} |
|
51
|
|
|
return Euro::newFromCents( |
|
52
|
|
|
intval( $currentFeeString ) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function newJsonErrorResponse( string $errorCode ): Response { |
|
57
|
|
|
if ( empty( self::ERROR_RESPONSE_MAP[$errorCode] ) ) { |
|
58
|
|
|
throw new \OutOfBoundsException( 'Validation error code not found' ); |
|
59
|
|
|
} |
|
60
|
|
|
return new JsonResponse( [ |
|
61
|
|
|
'status' => 'ERR', |
|
62
|
|
|
'messages' => [ |
|
63
|
|
|
'membershipFee' => self::ERROR_RESPONSE_MAP[$errorCode] |
|
64
|
|
|
] |
|
65
|
|
|
] ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|