Completed
Pull Request — master (#1319)
by Tonina
28:49
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\Fundraising\Frontend\BucketTesting\FeatureToggle;
8
9
/**
10
 * Factory for generating classes whose implementations differ due to A/B testing.
11
 *
12
 * @license GNU GPL v2+
13
 */
14
class ChoiceFactory {
15
16
	private $featureToggle;
17
18 93
	public function __construct( FeatureToggle $featureToggle ) {
19 93
		$this->featureToggle = $featureToggle;
20 93
	}
21
22 14
	public function isDonationAddressOptional(): bool {
23
		/** The "optional address" feature is only implemented for cat17 */
24
		if (
25 14
			$this->featureToggle->featureIsActive( 'campaigns.donation_address.required' ) ||
26 14
			$this->getSkinTemplateDirectory() !== $this->getSkinDirectory( 'cat17' )
27
		) {
28 11
			return false;
29 3
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.donation_address.optional' ) ) {
30 3
			return true;
31
		}
32
		throw new UnknownChoiceDefinition( 'Confirmation Page Template configuration failure.' );
33
	}
34
35 26
	public function isUsabilityImproved(): bool {
36
		/** The "improved usability" feature is only implemented for cat17 */
37
		if (
38 26
			$this->featureToggle->featureIsActive( 'campaigns.usability.old' ) ||
39 26
			$this->getSkinTemplateDirectory() !== $this->getSkinDirectory( 'cat17' )
40
		) {
41 26
			return false;
42
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.usability.improved' ) ) {
43
			return true;
44
		}
45
		throw new UnknownChoiceDefinition( 'Donation form usability configuration failure.' );
46
	}
47
48
49 93
	public function getSkinTemplateDirectory(): string {
50 93
		if ( $this->featureToggle->featureIsActive( 'campaigns.skins.cat17' ) ) {
51 14
			return $this->getSkinDirectory( 'cat17' );
52 79
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.10h16' ) ) {
53 1
			return $this->getSkinDirectory( '10h16' );
54 78
		} elseif ( $this->featureToggle->featureIsActive( 'campaigns.skins.test' ) ) {
55 78
			return $this->getSkinDirectory( 'test' );
56
		}
57
		throw new UnknownChoiceDefinition( 'Skin selection configuration failure.' );
58
	}
59
60 93
	private function getSkinDirectory( string $skin ): string {
61 93
		return 'skins/' . $skin . '/templates';
62
	}
63
}