Passed
Push — develop ( 6cb854...ee4d39 )
by Remco
03:23
created

LineItems::add_item()   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 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Line items.
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
15
/**
16
 * Line items.
17
 *
18
 * @author  Reüel van der Steege
19
 * @version 1.0.0
20
 * @since   1.0.0
21
 */
22
class LineItems {
23
	/**
24
	 * Line items.
25
	 *
26
	 * @var array
27
	 */
28
	private $line_items;
29
30
	/**
31
	 * Construct line items.
32
	 *
33
	 * @param LineItem[] $items Line items.
34
	 */
35 7
	public function __construct( $items = null ) {
36 7
		$this->line_items = array();
37
38 7
		if ( is_array( $items ) ) {
39 3
			foreach ( $items as $item ) {
40 2
				$this->add_item( $item );
41
			}
42
		}
43 7
	}
44
45
	/**
46
	 * Create and add new line item.
47
	 *
48
	 * @param string $description          Name.
49
	 * @param int    $quantity             Quantity.
50
	 * @param int    $amount_including_tax Amount (including tax).
51
	 *
52
	 * @return LineItem
53
	 *
54
	 * @throws InvalidArgumentException Throws invalid argument exception when arguments are invalid.
55
	 */
56 1
	public function new_item( $description, $quantity, $amount_including_tax ) {
57 1
		$item = new LineItem( $description, $quantity, $amount_including_tax );
58
59 1
		$this->add_item( $item );
60
61 1
		return $item;
62
	}
63
64
	/**
65
	 * Add line item.
66
	 *
67
	 * @param LineItem $item Line item.
68
	 * @return void
69
	 */
70 3
	public function add_item( LineItem $item ) {
71 3
		$this->line_items[] = $item;
72 3
	}
73
74
	/**
75
	 * Get line items.
76
	 *
77
	 * @return LineItem[]
78
	 */
79 7
	public function get_line_items() {
80 7
		return $this->line_items;
81
	}
82
83
	/**
84
	 * Get JSON.
85
	 *
86
	 * @return array|null
87
	 */
88 1
	public function get_json() {
89 1
		$data = array_map(
90 1
			function( LineItem $item ) {
91 1
				return $item->get_json();
92 1
			},
93 1
			$this->get_line_items()
94
		);
95
96 1
		return $data;
97
	}
98
}
99