Passed
Push — master ( 99a753...df0863 )
by Remco
19:34 queued 09:07
created

Money   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 58.06%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 112
ccs 18
cts 31
cp 0.5806
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

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