|
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
|
|
|
|