Amount   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 27
c 1
b 0
f 0
dl 0
loc 114
ccs 29
cts 30
cp 0.9667
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_currency() 0 2 1
A __construct() 0 3 1
A __toString() 0 2 1
A from_json() 0 16 2
A get_json() 0 4 1
A get_value() 0 2 1
A from_object() 0 12 3
1
<?php
2
/**
3
 * Amount
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use InvalidArgumentException;
14
use stdClass;
15
use JsonSchema\Constraints\Constraint;
16
use JsonSchema\Validator;
17
18
/**
19
 * Amount
20
 *
21
 * @author  Reüel van der Steege
22
 * @version 2.1.0
23
 * @since   2.1.0
24
 */
25
class Amount {
26
	/**
27
	 * Currency.
28
	 *
29
	 * @var string
30
	 */
31
	private $currency;
32
33
	/**
34
	 * Amount value.
35
	 *
36
	 * @var string
37
	 */
38
	private $value;
39
40
	/**
41
	 * Construct an amount.
42
	 *
43
	 * @param string $currency Currency code (ISO 4217).
44
	 * @param string $value    Amount formatted with correct number of decimals for currency.
45
	 */
46 9
	public function __construct( $currency, $value ) {
47 9
		$this->currency = $currency;
48 9
		$this->value    = $value;
49 9
	}
50
51
	/**
52
	 * Get currency.
53
	 *
54
	 * @return string
55
	 */
56 8
	public function get_currency() {
57 8
		return $this->currency;
58
	}
59
60
	/**
61
	 * Get amount.
62
	 *
63
	 * @return string
64
	 */
65 8
	public function get_value() {
66 8
		return $this->value;
67
	}
68
69
	/**
70
	 * Get JSON.
71
	 *
72
	 * @return object
73
	 */
74 5
	public function get_json() {
75
		return (object) array(
76 5
			'currency' => $this->get_currency(),
77 5
			'value'    => $this->get_value(),
78
		);
79
	}
80
81
	/**
82
	 * Create amount from object.
83
	 *
84
	 * @param stdClass $object Object.
85
	 *
86
	 * @return Amount
87
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
88
	 */
89 3
	public static function from_object( stdClass $object ) {
90 3
		if ( ! isset( $object->currency ) ) {
91 1
			throw new InvalidArgumentException( 'Object must contain `currency` property.' );
92
		}
93
94 2
		if ( ! isset( $object->value ) ) {
95 1
			throw new InvalidArgumentException( 'Object must contain `value` property.' );
96
		}
97
98 1
		return new self(
99 1
			$object->currency,
100 1
			$object->value
101
		);
102
	}
103
104
	/**
105
	 * Create amount from JSON string.
106
	 *
107
	 * @param object $json JSON object.
108
	 *
109
	 * @return Amount
110
	 *
111
	 * @throws InvalidArgumentException Throws invalid argument exception when input JSON is not an object.
112
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
113
	 */
114 1
	public static function from_json( $json ) {
115 1
		if ( ! is_object( $json ) ) {
116
			throw new InvalidArgumentException( 'JSON value must be an object.' );
117
		}
118
119 1
		$validator = new Validator();
120
121 1
		$validator->validate(
122 1
			$json,
123
			(object) array(
124 1
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/amount.json' ),
125
			),
126 1
			Constraint::CHECK_MODE_EXCEPTIONS
127
		);
128
129 1
		return self::from_object( $json );
130
	}
131
132
	/**
133
	 * Create string representation of amount.
134
	 *
135
	 * @return string
136
	 */
137 3
	public function __toString() {
138 3
		return sprintf( '%1$s %2$s', $this->get_currency(), $this->get_value() );
139
	}
140
}
141