Completed
Pull Request — master (#1659)
by Gabriel
88:01 queued 23:09
created

ChoiceFactory::getUseOfFundsResponse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.0313

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 1
dl 0
loc 25
ccs 7
cts 19
cp 0.3684
crap 8.0313
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Factories;
6
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use WMDE\Euro\Euro;
10
use WMDE\Fundraising\Frontend\BucketTesting\FeatureToggle;
11
use WMDE\Fundraising\Frontend\Infrastructure\UrlGenerator;
12
13
/**
14
 * Factory for generating classes whose implementations differ due to A/B testing.
15
 *
16
 * @license GNU GPL v2+
17
 */
18
class ChoiceFactory {
19
20 100
	private $featureToggle;
21 100
22 100
	public function __construct( FeatureToggle $featureToggle ) {
23
		$this->featureToggle = $featureToggle;
24 14
	}
25
26 14
	public function isDonationAddressOptional(): bool {
27 10
		if ( $this->featureToggle->featureIsActive( 'campaigns.donation_address.required' ) ) {
28
			return false;
29 4
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.donation_address.optional' ) ) {
30 4
			return true;
31
		}
32
		throw new UnknownChoiceDefinition( 'Confirmation Page Template configuration failure.' );
33
	}
34
35
	public function getSkinTemplateDirectory(): string {
36
		if ( $this->featureToggle->featureIsActive( 'campaigns.skins.laika' ) ) {
37 100
			return $this->getSkinDirectory( 'laika' );
38 100
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.10h16' ) ) {
39 19
			return $this->getSkinDirectory( '10h16' );
40 81
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.test' ) ) {
41 1
			return $this->getSkinDirectory( 'test' );
42 80
		}
43 80
		throw new UnknownChoiceDefinition( 'Skin selection configuration failure.' );
44
	}
45
46
	public function getUseOfFundsResponse( FunFunFactory $factory ): \Closure {
47
		if ( $this->featureToggle->featureIsActive( 'campaigns.skins.laika' ) ) {
48 15
			$factory->getTranslationCollector()->addTranslationFile( $factory->getI18nDirectory() . '/messages/useOfFundsMessages.json' );
49 15
			$template = $factory->getLayoutTemplate( 'Funds_Usage.html.twig', [
50 15
				'use_of_funds_content' => $factory->getApplicationOfFundsContent(),
51
				'use_of_funds_messages' => $factory->getApplicationOfFundsMessages()
52
			] );
53
			return function() use ( $template )  {
54
				return $template->render( [] );
55
			};
56
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.10h16' ) ) {
57 1
			// Redirect to laika skin, since we don't have usa of fund in 10h16
58 1
			return function( Request $request ) {
59
				$params = $request->query->all();
60 1
				$params['skin'] = '0';
61 1
				$url = $request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().'?'.http_build_query( $params );
62
				return new RedirectResponse( $url );
63
			};
64
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.test' ) ) {
65
			// we don't care what happens in test
66
			return function() { return 'Test rendering: Use of funds';
67
68
   };
69
		}
70
		throw new UnknownChoiceDefinition( 'Use of funds configuration failure.' );
71
	}
72
73
	public function getMembershipCallToActionTemplate(): string {
74 100
		if ( $this->featureToggle->featureIsActive( 'campaigns.membership_call_to_action.regular' ) ) {
75 100
			return 'partials/donation_confirmation/feature_toggle/call_to_action_regular.html.twig';
76
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.membership_call_to_action.video' ) ) {
77
			return 'partials/donation_confirmation/feature_toggle/call_to_action_video.html.twig';
78 1
		}
79
		throw new UnknownChoiceDefinition( 'Membership Call to Action Template configuration failure.' );
80 1
	}
81 1
82
	public function getAmountOption(): array {
83
		if ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to300_0' ) ) {
84
			return $this->getAmountOptionInEuros( [ 500, 1500, 2500, 5000, 7500, 10000, 25000, 30000 ] );
85
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to300' ) ) {
86
			return $this->getAmountOptionInEuros( [ 500, 1500, 2500, 5000, 7500, 10000, 25000, 30000 ] );
87
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.5to100' ) ) {
88
			return $this->getAmountOptionInEuros( [ 500, 1000, 1500, 2000, 3000, 5000, 7500, 10000 ] );
89
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.15to250' ) ) {
90
			return $this->getAmountOptionInEuros( [ 1500, 2000, 2500, 3000, 5000, 7500, 10000, 25000 ] );
91
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.30to250' ) ) {
92
			return $this->getAmountOptionInEuros( [ 3000, 4000, 5000, 7500, 10000, 15000, 20000, 25000 ] );
93
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.amount_options.50to500' ) ) {
94
			return $this->getAmountOptionInEuros( [ 5000, 10000, 15000, 20000, 25000, 30000, 50000 ] );
95
		}
96
		throw new UnknownChoiceDefinition( 'Amount option selection configuration failure.' );
97
	}
98
99
	private function getSkinDirectory( string $skin ): string {
100
		return 'skins/' . $skin . '/templates';
101
	}
102
103
	public function getAmountOptionInEuros( array $amountOption ): array {
104
		return array_map( function ( int $amount ) {
105
			return Euro::newFromCents( $amount );
106
		}, $amountOption );
107
	}
108
}