Passed
Push — feature/post-pay ( 12bb6b...7e10da )
by Remco
04:44
created

OrderAnnounceResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 83
ccs 20
cts 24
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A from_json() 0 14 1
A from_object() 0 12 3
A __construct() 0 4 1
A get_signature_fields() 0 3 1
A get_redirect_url() 0 2 1
1
<?php
2
/**
3
 * Order announce response
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 JsonSchema\Constraints\Constraint;
15
use JsonSchema\Exception\ValidationException;
16
use JsonSchema\Validator;
17
18
/**
19
 * Order announce response
20
 *
21
 * @author  Remco Tolsma
22
 * @version 2.0.2
23
 * @since   2.0.2
24
 */
25
class OrderAnnounceResponse extends ResponseMessage {
26
	/**
27
	 * Redirect URL.
28
	 *
29
	 * @var string
30
	 */
31
	private $redirect_url;
32
33
	/**
34
	 * Construct notification message.
35
	 *
36
	 * @param string $redirect_url Redirect URL.
37
	 * @param string $signature    Signature.
38
	 */
39 1
	public function __construct( $redirect_url, $signature ) {
40 1
		parent::__construct( $signature );
41
42 1
		$this->redirect_url = $redirect_url;
43 1
	}
44
45
	/**
46
	 * Get redirect URL.
47
	 *
48
	 * @return string
49
	 */
50 1
	public function get_redirect_url() {
51 1
		return $this->redirect_url;
52
	}
53
54
	/**
55
	 * Get signature fields.
56
	 *
57
	 * @return array
58
	 */
59
	public function get_signature_fields() {
60
		return array(
61
			$this->get_redirect_url(),
62
		);
63
	}
64
65
	/**
66
	 * Create notification from object.
67
	 *
68
	 * @param object $object Object.
69
	 * @return OrderAnnounceResponse
70
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
71
	 */
72 1
	public static function from_object( $object ) {
73 1
		if ( ! isset( $object->signature ) ) {
74
			throw new InvalidArgumentException( 'Object must contain `signature` property.' );
75
		}
76
77 1
		if ( ! isset( $object->redirectUrl ) ) {
78
			throw new InvalidArgumentException( 'Object must contain `redirectUrl` property.' );
79
		}
80
81 1
		return new self(
82 1
			$object->redirectUrl,
83 1
			$object->signature
84
		);
85
	}
86
87
	/**
88
	 * Create order announce response from JSON string.
89
	 *
90
	 * @param string $json JSON string.
91
	 * @return OrderAnnounceResponse
92
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
93
	 */
94 1
	public static function from_json( $json ) {
95 1
		$data = json_decode( $json );
96
97 1
		$validator = new Validator();
98
99 1
		$validator->validate(
100 1
			$data,
101
			(object) array(
102 1
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/order-announce-response.json' ),
103
			),
104 1
			Constraint::CHECK_MODE_EXCEPTIONS
105
		);
106
107 1
		return self::from_object( $data );
108
	}
109
}
110