Completed
Pull Request — master (#1955)
by
unknown
292:33 queued 227:33
created

ValidateFeeController::newJsonErrorResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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() );
0 ignored issues
show
Bug introduced by
It seems like $response->getErrorCode() can also be of type null; however, parameter $errorCode of WMDE\Fundraising\Fronten...:newJsonErrorResponse() 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

43
		return $this->newJsonErrorResponse( /** @scrutinizer ignore-type */ $response->getErrorCode() );
Loading history...
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