Items::get_amount()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 18
ccs 5
cts 5
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\IDealBasic;
4
5
use ArrayIterator;
6
use IteratorAggregate;
7
use Pronamic\WordPress\Money\Money;
8
9
/**
10
 * Title: iDEAL Basic items
11
 * Description:
12
 * Copyright: 2005-2021 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.0.0
17
 * @since   1.0.0
18
 */
19
class Items implements IteratorAggregate {
20
	/**
21
	 * The items
22
	 *
23
	 * @var Item[]
24
	 */
25
	private $items;
26
27
	/**
28
	 * Constructs and initialize a iDEAL basic object
29
	 *
30 3
	 * @return void
31 3
	 */
32 3
	public function __construct() {
33
		$this->items = array();
34
	}
35
36
	/**
37
	 * Get iterator
38
	 *
39
	 * @see IteratorAggregate::getIterator()
40
	 * @return ArrayIterator
41 1
	 */
42
	// @codingStandardsIgnoreStart
43 1
	// Function name "getIterator" is in camel caps format, try 'get_iterator'
44
	public function getIterator() {
45
		// @codingStandardsIgnoreEnd
46
		return new ArrayIterator( $this->items );
47
	}
48
49
	/**
50
	 * Add item
51 2
	 *
52 2
	 * @param Item $item Item.
53 2
	 * @return void
54
	 */
55
	public function add_item( Item $item ) {
56
		$this->items[] = $item;
57
	}
58 2
59 2
	/**
60
	 * Calculate the total amount of all items
61 2
	 *
62
	 * @return Money
63 2
	 */
64 2
	public function get_amount() {
65
		$amount = 0;
66
67 2
		$use_bcmath = extension_loaded( 'bcmath' );
68
69 2
		foreach ( $this->items as $item ) {
70
			if ( $use_bcmath ) {
71 2
				// Use non-locale aware float value.
72
				// @link http://php.net/sprintf.
73
				$item_amount = sprintf( '%F', $item->get_amount() );
74
75 2
				$amount = bcadd( $amount, $item_amount, 8 );
76
			} else {
77
				$amount += $item->get_amount();
78
			}
79
		}
80
81
		return new Money( $amount );
82
	}
83
}
84