Completed
Pull Request — master (#1403)
by Tonina
27:56
created

ChoiceFactory::getSkinDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
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
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 14
			return $this->getSkinDirectory( 'cat17' );
39 81
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.10h16' ) ) {
40 2
			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 9
			return 'partials/donation_confirmation/feature_toggle/call_to_action_regular.html.twig';
50 6
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.membership_call_to_action.video' ) ) {
51 6
			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 2
	public function getAmountOption(): array {
57 2
		if ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to300' ) ) {
58 2
			return $this->getAmountOptionInEuros( [ 500, 1500, 2500, 5000, 7500, 10000, 25000, 30000 ] );
59
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to100' ) ) {
60
			return $this->getAmountOptionInEuros( [ 500, 1000, 1500, 2000, 3000, 5000, 7500, 10000 ] );
61
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.15to250' ) ) {
62
			return $this->getAmountOptionInEuros( [ 1500, 2000, 2500, 3000, 5000, 7500, 10000, 25000 ] );
63
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.30to250' ) ) {
64
			return $this->getAmountOptionInEuros( [ 3000, 4000, 5000, 7500, 10000, 15000, 20000, 25000 ] );
65
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.50to500' ) ) {
66
			return $this->getAmountOptionInEuros( [ 5000, 10000, 15000, 20000, 25000, 30000, 50000 ] );
67
		}
68
		throw new UnknownChoiceDefinition( 'Amount option selection configuration failure.' );
69
	}
70
71 95
	private function getSkinDirectory( string $skin ): string {
72 95
		return 'skins/' . $skin . '/templates';
73
	}
74
75 2
	public function getAmountOptionInEuros( array $amountOption ): array {
76
		return array_map( function ( int $amount ) {
77 2
			return Euro::newFromCents( $amount );
78 2
		}, $amountOption );
79
	}
80
}