Chargeback::from_json()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 19
ccs 0
cts 15
cp 0
crap 2
rs 9.9
1
<?php
2
/**
3
 * Chargeback
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 DateTimeInterface;
14
15
/**
16
 * Chargeback
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.1.0
20
 * @since   2.1.0
21
 */
22
class Chargeback extends BaseResource {
23
	/**
24
	 * The amount charged back by the consumer.
25
	 *
26
	 * @var Amount
27
	 */
28
	private $amount;
29
30
	/**
31
	 * The date and time the chargeback was issued.
32
	 *
33
	 * @var DateTimeInterface
34
	 */
35
	private $created_at;
36
37
	/**
38
	 * Construct chargeback.
39
	 *
40
	 * @param string            $id            Identifier.
41
	 * @param Amount            $amount        Amount.
42
	 * @param DateTimeInterface $created_at    Created at.
43
	 */
44
	public function __construct( $id, Amount $amount, DateTimeInterface $created_at ) {
45
		parent::__construct( $id );
46
47
		$this->amount     = $amount;
48
		$this->created_at = $created_at;
49
	}
50
51
	/**
52
	 * Get created at.
53
	 *
54
	 * @return DateTimeInterface
55
	 */
56
	public function get_created_at() {
57
		return $this->created_at;
58
	}
59
60
	/**
61
	 * Create chargeback from JSON.
62
	 *
63
	 * @link https://docs.mollie.com/reference/v2/chargebacks-api/get-chargeback
64
	 * @param object $json JSON object.
65
	 * @return Chargeback
66
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
67
	 */
68
	public static function from_json( $json ) {
69
		$validator = new \JsonSchema\Validator();
70
71
		$validator->validate(
72
			$json,
73
			(object) array(
74
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/chargeback.json' ),
75
			),
76
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
77
		);
78
79
		$chargeback = new Chargeback(
80
			$json->id,
81
			Amount::from_json( $json->amount ),
82
			// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
83
			new \DateTimeImmutable( $json->createdAt )
84
		);
85
86
		return $chargeback;
87
	}
88
}
89