Failed Conditions
Push — develop ( 05bf6b...a342cf )
by Reüel
03:02
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 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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 array $items Line items.
34
	 */
35
	public function __construct( $items = null ) {
36
		if ( is_array( $items ) ) {
37
			foreach ( $items as $item ) {
38
				$this->add_item( $item );
39
			}
40
		}
41
	}
42
43
	/**
44
	 * Create and add new line item.
45
	 *
46
	 * @param string $name     Name.
47
	 * @param int    $quantity Quantity.
48
	 * @param Amount $amount   Amount.
49
	 * @param string $category Category.
50
	 *
51
	 * @return LineItem
52
	 *
53
	 * @throws InvalidArgumentException Throws invalid argument exception when arguments are invalid.
54
	 */
55
	public function new_item( $name, $quantity, Amount $amount, $category ) {
56
		$item = new LineItem( $name, $quantity, $amount, $category );
57
58
		$this->add_item( $item );
59
60
		return $item;
61
	}
62
63
	/**
64
	 * Add line item.
65
	 *
66
	 * @param LineItem $item Line item.
67
	 */
68
	public function add_item( LineItem $item ) {
69
		$this->line_items[] = $item;
70
	}
71
72
	/**
73
	 * Get line items.
74
	 *
75
	 * @return LineItem[]
76
	 */
77
	public function get_line_items() {
78
		return $this->line_items;
79
	}
80
81
	/**
82
	 * Get JSON.
83
	 *
84
	 * @return array|null
85
	 */
86
	public function get_json() {
87
		$data = array_map(
88
			function( LineItem $item ) {
89
				return $item->get_json();
90
			},
91
			$this->get_line_items()
92
		);
93
94
		return $data;
95
	}
96
}
97