Failed Conditions
Push — develop ( 5f9818...c59aaa )
by Remco
03:38
created

Amount::get_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use InvalidArgumentException;
14
use JsonSchema\Constraints\Constraint;
15
use JsonSchema\Exception\ValidationException;
16
use JsonSchema\Validator;
17
18
/**
19
 * Amount
20
 *
21
 * @link https://docs.adyen.com/developers/api-reference/common-api/amount
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.0
25
 * @since   1.0.0
26
 */
27
class Amount {
28
	/**
29
	 * Currency.
30
	 *
31
	 * @var string
32
	 */
33
	private $currency;
34
35
	/**
36
	 * Value.
37
	 *
38
	 * @var int
39
	 */
40
	private $value;
41
42
	/**
43
	 * Construct amount.
44
	 *
45
	 * @param string $currency Currency.
46
	 * @param int    $value    Value.
47
	 *
48
	 * @throws InvalidArgumentException Throws invalid argument exception when Adyen amount requirements are not met.
49
	 */
50 11
	public function __construct( $currency, $value ) {
51 11
		if ( 3 !== strlen( $currency ) ) {
52 1
			throw new InvalidArgumentException(
53 1
				sprintf(
54 1
					'Given currency `%s` not a three-character ISO currency code.',
55 1
					$currency
56
				)
57
			);
58
		}
59
60 10
		if ( ! is_int( $value ) ) {
0 ignored issues
show
introduced by
The condition is_int($value) is always true.
Loading history...
61 1
			throw new InvalidArgumentException(
62 1
				sprintf(
63 1
					'Given value `%s` is not an integer.',
64 1
					$value
65
				)
66
			);
67
		}
68
69 9
		$this->currency = $currency;
70 9
		$this->value    = $value;
71 9
	}
72
73
	/**
74
	 * Get currency.
75
	 *
76
	 * @return string
77
	 */
78 5
	public function get_currency() {
79 5
		return $this->currency;
80
	}
81
82
	/**
83
	 * Get amount.
84
	 *
85
	 * @return int
86
	 */
87 5
	public function get_value() {
88 5
		return $this->value;
89
	}
90
91
	/**
92
	 * Get JSON.
93
	 *
94
	 * @return object
95
	 */
96 2
	public function get_json() {
97
		return (object) array(
98 2
			'currency' => $this->get_currency(),
99 2
			'value'    => $this->get_value(),
100
		);
101
	}
102
103
	/**
104
	 * Create amount from object.
105
	 *
106
	 * @param object $object Object.
107
	 * @return Amount
108
	 * @throws ValidationException Throws validation exception when object does not contains the required properties.
109
	 */
110 7
	public static function from_object( $object ) {
111 7
		$validator = new Validator();
112
113 7
		$validator->validate(
114 7
			$object,
115
			(object) array(
116 7
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/amount.json' ),
117
			),
118 7
			Constraint::CHECK_MODE_EXCEPTIONS
119
		);
120
121 5
		return new self(
122 5
			$object->currency,
123 5
			$object->value
124
		);
125
	}
126
}
127