Message   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 115
ccs 0
cts 24
cp 0
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A add_elements() 0 3 2
A add_element() 0 10 2
A set_create_date() 0 2 1
A get_create_date() 0 2 1
A __construct() 0 3 1
A get_name() 0 2 1
1
<?php
2
/**
3
 * Message.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\IDealAdvancedV3\XML;
12
13
use DateTimeImmutable;
14
use DateTimeZone;
15
use DOMDocument;
16
use DOMNode;
17
use DOMText;
18
use Pronamic\WordPress\Pay\Plugin;
19
20
/**
21
 * Title: iDEAL XML message
22
 * Description:
23
 * Copyright: 2005-2021 Pronamic
24
 * Company: Pronamic
25
 *
26
 * @author  Remco Tolsma
27
 * @version 2.0.0
28
 */
29
class Message {
30
	/**
31
	 * The XML version of the iDEAL messages
32
	 *
33
	 * @var string
34
	 */
35
	const XML_VERSION = '1.0';
36
37
	/**
38
	 * The XML encoding of the iDEAL messages
39
	 *
40
	 * @var string
41
	 */
42
	const XML_ENCODING = 'UTF-8';
43
44
	/**
45
	 * The XML namespace of the iDEAL messages
46
	 *
47
	 * @var string
48
	 */
49
	const XML_NAMESPACE = 'http://www.idealdesk.com/ideal/messages/mer-acq/3.3.1';
50
51
	/**
52
	 * The version of the iDEAL messages
53
	 *
54
	 * @var string
55
	 */
56
	const VERSION = '3.3.1';
57
58
	/**
59
	 * The name of this message
60
	 *
61
	 * @var string
62
	 */
63
	private $name;
64
65
	/**
66
	 * The create date of this message
67
	 *
68
	 * @var DateTimeImmutable
69
	 */
70
	private $create_date;
71
72
	/**
73
	 * Constructs and initialize an message
74
	 *
75
	 * @param string $name Message name.
76
	 * @return void
77
	 * @throws \Exception Throws exception on date error.
78
	 */
79
	public function __construct( $name ) {
80
		$this->name        = $name;
81
		$this->create_date = new DateTimeImmutable( 'now', new DateTimeZone( Plugin::TIMEZONE ) );
82
	}
83
84
	/**
85
	 * Get the name of this message
86
	 *
87
	 * @return string
88
	 */
89
	public function get_name() {
90
		return $this->name;
91
	}
92
93
	/**
94
	 * Get the create date
95
	 *
96
	 * @return DateTimeImmutable
97
	 */
98
	public function get_create_date() {
99
		return $this->create_date;
100
	}
101
102
	/**
103
	 * Set the create date
104
	 *
105
	 * @param DateTimeImmutable $create_date Date created.
106
	 * @return void
107
	 */
108
	public function set_create_date( DateTimeImmutable $create_date ) {
109
		$this->create_date = $create_date;
110
	}
111
112
	/**
113
	 * Create and add an element with the specified name and value to the specified parent
114
	 *
115
	 * @param DOMDocument $document Document.
116
	 * @param DOMNode     $parent   Parent.
117
	 * @param string      $name     Element name.
118
	 * @param string|null $value    Element text value.
119
	 * @return \DOMElement
120
	 */
121
	public static function add_element( DOMDocument $document, DOMNode $parent, $name, $value = null ) {
122
		$element = $document->createElement( $name );
123
124
		if ( null !== $value ) {
125
			$element->appendChild( new DOMText( $value ) );
126
		}
127
128
		$parent->appendChild( $element );
129
130
		return $element;
131
	}
132
133
	/**
134
	 * Add the specified elements to the parent node
135
	 *
136
	 * @param DOMDocument                $document Document.
137
	 * @param DOMNode                    $parent   Parent.
138
	 * @param array<string, string|null> $elements Elements to add.
139
	 * @return void
140
	 */
141
	public static function add_elements( DOMDocument $document, DOMNode $parent, array $elements = array() ) {
142
		foreach ( $elements as $name => $value ) {
143
			self::add_element( $document, $parent, $name, $value );
144
		}
145
	}
146
}
147