Completed
Push — master ( b5c370...38fd6d )
by Gabriel
11:46 queued 11s
created

ChoiceFactory::isDonationAddressOptional()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

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