Completed
Push — master ( 76b128...063ba4 )
by Jeroen De
108:30 queued 90:40
created

AmountPolicyValidator::isRecurringAmountTooHigh()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Validation;
6
7
/**
8
 * @licence GNU GPL v2+
9
 * @author Kai Nissen < [email protected] >
10
 */
11
class AmountPolicyValidator {
12
13
	private $maxAmountOneTime;
14
	private $maxAmountRecurringAnnually;
15
16
	const VIOLATION_TOO_HIGH = 'too_high';
17
18 25
	public function __construct( int $maxAmountOneTime, int $maxAmountRecurringAnnually ) {
19 25
		$this->maxAmountOneTime = $maxAmountOneTime;
20 25
		$this->maxAmountRecurringAnnually = $maxAmountRecurringAnnually;
21 25
	}
22
23 22
	public function validate( float $amount, int $interval ): ValidationResult {
24 22
		if ( $this->isOneTimeAmountTooHigh( $amount, $interval ) ||
25 22
			$this->isAnuallyRecurringAmountTooHigh( $amount, $interval ) ) {
26
27 5
			return new ValidationResult( new ConstraintViolation( $amount, self::VIOLATION_TOO_HIGH ) );
28
		}
29
30 17
		return new ValidationResult();
31
	}
32
33 22
	private function isOneTimeAmountTooHigh( float $amount, int $interval ): bool {
34 22
		if ( $interval === 0 ) {
35 11
			return $amount >= $this->maxAmountOneTime;
36
		}
37 11
		return false;
38
	}
39
40 21
	private function isAnuallyRecurringAmountTooHigh( float $amount, int $interval ): bool {
41 21
		if ( $interval > 0 ) {
42 11
			return ( 12 / $interval ) * $amount >= $this->maxAmountRecurringAnnually;
43
		}
44 10
		return false;
45
	}
46
47
}
48