Completed
Push — master ( df16dc...b3003a )
by Tonina
39:26 queued 31:15
created

ChoiceFactory::getAmountOption()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 10.8579

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 7
nop 0
dl 0
loc 15
ccs 8
cts 14
cp 0.5714
crap 10.8579
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Factories;
6
7
use WMDE\Euro\Euro;
8
use WMDE\Fundraising\Frontend\BucketTesting\FeatureToggle;
9
10
/**
11
 * Factory for generating classes whose implementations differ due to A/B testing.
12
 *
13
 * @license GNU GPL v2+
14
 */
15
class ChoiceFactory {
16
17
	private $featureToggle;
18
19 95
	public function __construct( FeatureToggle $featureToggle ) {
20 95
		$this->featureToggle = $featureToggle;
21 95
	}
22
23 14
	public function isDonationAddressOptional(): bool {
24
		/** The "optional address" feature is only implemented for cat17 */
25 14
		if ( $this->getSkinTemplateDirectory() !== $this->getSkinDirectory( 'cat17' ) ) {
26 10
			return false;
27
		}
28 4
		if ( $this->featureToggle->featureIsActive( 'campaigns.donation_address.required' ) ) {
29 4
			return false;
30
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.donation_address.optional' ) ) {
31
			return true;
32
		}
33
		throw new UnknownChoiceDefinition( 'Confirmation Page Template configuration failure.' );
34
	}
35
36 95
	public function getSkinTemplateDirectory(): string {
37 95
		if ( $this->featureToggle->featureIsActive( 'campaigns.skins.cat17' ) ) {
38 15
			return $this->getSkinDirectory( 'cat17' );
39 80
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.10h16' ) ) {
40 1
			return $this->getSkinDirectory( '10h16' );
41 79
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.test' ) ) {
42 79
			return $this->getSkinDirectory( 'test' );
43
		}
44
		throw new UnknownChoiceDefinition( 'Skin selection configuration failure.' );
45
	}
46
47 15
	public function getMembershipCallToActionTemplate(): string {
48 15
		if ( $this->featureToggle->featureIsActive( 'campaigns.membership_call_to_action.regular' ) ) {
49 3
			return 'partials/donation_confirmation/feature_toggle/call_to_action_regular.html.twig';
50 12
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.membership_call_to_action.video' ) ) {
51 12
			return 'partials/donation_confirmation/feature_toggle/call_to_action_video.html.twig';
52
		}
53
		throw new UnknownChoiceDefinition( 'Membership Call to Action Template configuration failure.' );
54
	}
55
56 1
	public function getAmountOption(): array {
57 1
		if ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to300_0' ) ) {
58
			return $this->getAmountOptionInEuros( [ 500, 1500, 2500, 5000, 7500, 10000, 25000, 30000 ] );
59 1
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to300' ) ) {
60
			return $this->getAmountOptionInEuros( [ 500, 1500, 2500, 5000, 7500, 10000, 25000, 30000 ] );
61 1
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to100' ) ) {
62
			return $this->getAmountOptionInEuros( [ 500, 1000, 1500, 2000, 3000, 5000, 7500, 10000 ] );
63 1
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.15to250' ) ) {
64
			return $this->getAmountOptionInEuros( [ 1500, 2000, 2500, 3000, 5000, 7500, 10000, 25000 ] );
65 1
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.30to250' ) ) {
66
			return $this->getAmountOptionInEuros( [ 3000, 4000, 5000, 7500, 10000, 15000, 20000, 25000 ] );
67 1
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.50to500' ) ) {
68 1
			return $this->getAmountOptionInEuros( [ 5000, 10000, 15000, 20000, 25000, 30000, 50000 ] );
69
		}
70
		throw new UnknownChoiceDefinition( 'Amount option selection configuration failure.' );
71
	}
72
73 95
	private function getSkinDirectory( string $skin ): string {
74 95
		return 'skins/' . $skin . '/templates';
75
	}
76
77 1
	public function getAmountOptionInEuros( array $amountOption ): array {
78
		return array_map( function ( int $amount ) {
79 1
			return Euro::newFromCents( $amount );
80 1
		}, $amountOption );
81
	}
82
}