Failed Conditions
Push — develop ( 511b66...00af81 )
by Remco
02:50
created

Amount   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 75
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A from_object() 0 12 3
A __construct() 0 3 1
A get_value() 0 2 1
A get_currency() 0 2 1
A get_json() 0 4 1
1
<?php
2
/**
3
 * Amount
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use InvalidArgumentException;
14
15
/**
16
 * Amount
17
 *
18
 * @link https://docs.adyen.com/developers/api-reference/common-api/amount
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.0
22
 * @since   1.0.0
23
 */
24
class Amount {
25
	/**
26
	 * Currency.
27
	 *
28
	 * @var string
29
	 */
30
	private $currency;
31
32
	/**
33
	 * Value.
34
	 *
35
	 * @var int
36
	 */
37
	private $value;
38
39
	/**
40
	 * Construct amount.
41
	 *
42
	 * @param string $currency Currency.
43
	 * @param int    $value    Value.
44
	 */
45 4
	public function __construct( $currency, $value ) {
46 4
		$this->currency = $currency;
47 4
		$this->value    = $value;
48 4
	}
49
50
	/**
51
	 * Get currency.
52
	 *
53
	 * @return string
54
	 */
55 2
	public function get_currency() {
56 2
		return $this->currency;
57
	}
58
59
	/**
60
	 * Get amount.
61
	 *
62
	 * @return int
63
	 */
64 2
	public function get_value() {
65 2
		return $this->value;
66
	}
67
68
	/**
69
	 * Get JSON.
70
	 *
71
	 * @return object
72
	 */
73 1
	public function get_json() {
74
		return (object) array(
75 1
			'currency' => $this->get_currency(),
76 1
			'value'    => $this->get_value(),
77
		);
78
	}
79
80
	/**
81
	 * Create amount from object.
82
	 *
83
	 * @param object $object Object.
84
	 * @return Amount
85
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
86
	 */
87 2
	public static function from_object( $object ) {
88 2
		if ( ! isset( $object->currency ) ) {
89
			throw new InvalidArgumentException( 'Object must contain `currency` property.' );
90
		}
91
92 2
		if ( ! isset( $object->value ) ) {
93
			throw new InvalidArgumentException( 'Object must contain `value` property.' );
94
		}
95
96 2
		return new self(
97 2
			$object->currency,
98 2
			$object->value
99
		);
100
	}
101
}
102