Completed
Pull Request — master (#971)
by wiese
94:17 queued 29:21
created

PaymentTypesSettings::updateSetting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Presentation;
6
7
use InvalidArgumentException;
8
9
/**
10
 * Class PaymentTypesSettings
11
 * @package WMDE\Fundraising\Frontend\Presentation
12
 *
13
 * Takes a config like the following and provides read and write interface
14
 *
15
 * [
16
 *   'BEZ' => [
17
 *     PaymentTypesSettings::PURPOSE_DONATION => true,
18
 *     PaymentTypesSettings::PURPOSE_MEMBERSHIP => true
19
 *   ],
20
 *   'UEB' => [
21
 *     PaymentTypesSettings::PURPOSE_DONATION => true,
22
 *     PaymentTypesSettings::PURPOSE_MEMBERSHIP => false
23
 *   ]
24
 * ]
25
 */
26
class PaymentTypesSettings {
27
28
	public const PURPOSE_DONATION = 'donation-enabled';
29
	public const PURPOSE_MEMBERSHIP = 'membership-enabled';
30
31
	private $settings = [];
32
33
	public function __construct( array $settings ) {
34
		$this->settings = $settings;
35
	}
36
37
	/**
38
	 * @return string[]
39
	 */
40
	public function getEnabledForDonation(): array {
41
		return $this->getEnabledTypes( self::PURPOSE_DONATION );
42
	}
43
44
	/**
45
	 * @return string[]
46
	 */
47
	public function getEnabledForMembershipApplication(): array {
48
		return $this->getEnabledTypes( self::PURPOSE_MEMBERSHIP );
49
	}
50
51
	public function updateSetting( string $paymentType, string $purpose, bool $value ): void {
52
		if ( !array_key_exists( $paymentType, $this->settings ) ) {
53
			throw new InvalidArgumentException( "Can not update setting of unknown paymentType '$paymentType'." );
54
		}
55
		if ( !array_key_exists( $purpose, $this->settings[$paymentType] ) ) {
56
			throw new InvalidArgumentException( "Can not update setting of unknown purpose '$purpose'." );
57
		}
58
59
		$this->settings[$paymentType][$purpose] = $value;
60
	}
61
62
	/**
63
	 * @return string[]
64
	 */
65
	private function getEnabledTypes( string $purpose ): array {
66
		return array_keys( array_filter( $this->settings, function ( $config ) use ( $purpose ) {
67
			return ( $config[$purpose] ?? false ) === true;
68
		} ) );
69
	}
70
}
71
72