Completed
Push — master ( e01c3b...e3b9fc )
by Jeroen De
04:48 queued 40s
created

PayPalUrlConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 6
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Presentation;
6
7
/**
8
 * @licence GNU GPL v2+
9
 * @author Kai Nissen < [email protected] >
10
 */
11
class PayPalUrlConfig {
12
13
	const CONFIG_KEY_ACCOUNT_ADDRESS = 'account-address';
14
	const CONFIG_KEY_BASE_URL = 'base-url';
15
	const CONFIG_KEY_NOTIFY_URL = 'notify-url';
16
	const CONFIG_KEY_RETURN_URL = 'return-url';
17
	const CONFIG_KEY_CANCEL_URL = 'cancel-url';
18
	const CONFIG_KEY_ITEM_NAME = 'item-name';
19
20
	private $payPalAccountAddress;
21
	private $payPalBaseUrl;
22
	private $notifyUrl;
23
	private $returnUrl;
24
	private $cancelUrl;
25
	private $itemName;
26
27
	private function __construct( string $payPalAccountAddress, string $payPalBaseUrl, string $notifyUrl,
28
								 string $returnUrl, string $cancelUrl, string $itemName ) {
29
		$this->payPalAccountAddress = $payPalAccountAddress;
30
		$this->payPalBaseUrl = $payPalBaseUrl;
31
		$this->notifyUrl = $notifyUrl;
32
		$this->returnUrl = $returnUrl;
33
		$this->cancelUrl = $cancelUrl;
34
		$this->itemName = $itemName;
35
	}
36
37
	/**
38
	 * @param string[] $config
39
	 * @return PayPalUrlConfig
40
	 * @throws \RuntimeException
41
	 */
42
	public static function newFromConfig( array $config ): self {
43
		return ( new PayPalUrlConfig(
44
			$config[self::CONFIG_KEY_ACCOUNT_ADDRESS],
45
			$config[self::CONFIG_KEY_BASE_URL],
46
			$config[self::CONFIG_KEY_NOTIFY_URL],
47
			$config[self::CONFIG_KEY_RETURN_URL],
48
			$config[self::CONFIG_KEY_CANCEL_URL],
49
			$config[self::CONFIG_KEY_ITEM_NAME]
50
		) )->assertNoEmptyFields();
51
	}
52
53
	private function assertNoEmptyFields(): self {
54
		foreach ( get_object_vars( $this ) as $fieldName => $fieldValue ) {
55
			if ( empty( $fieldValue ) ) {
56
				throw new \RuntimeException( "Configuration variable '$fieldName' can not be empty" );
57
			}
58
		}
59
60
		return $this;
61
	}
62
63
	public function getPayPalAccountAddress(): string {
64
		return $this->payPalAccountAddress;
65
	}
66
67
	public function getPayPalBaseUrl(): string {
68
		return $this->payPalBaseUrl;
69
	}
70
71
	public function getNotifyUrl(): string {
72
		return $this->notifyUrl;
73
	}
74
75
	public function getReturnUrl(): string {
76
		return $this->returnUrl;
77
	}
78
79
	public function getCancelUrl(): string {
80
		return $this->cancelUrl;
81
	}
82
83
	public function getItemName(): string {
84
		return $this->itemName;
85
	}
86
87
}
88