Failed Conditions
Push — develop ( e866ac...7d92e9 )
by Reüel
11:11
created

Items::get_amount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
crap 3.0123
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-2019 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 array
24
	 */
25
	private $items;
26
27
	/**
28
	 * Constructs and initialize a iDEAL basic object
29
	 */
30 3
	public function __construct() {
31 3
		$this->items = array();
32 3
	}
33
34
	/**
35
	 * Get iterator
36
	 *
37
	 * @see IteratorAggregate::getIterator()
38
	 */
39
	// @codingStandardsIgnoreStart
40
	// Function name "getIterator" is in camel caps format, try 'get_iterator'
41 1
	public function getIterator() {
42
		// @codingStandardsIgnoreEnd
43 1
		return new ArrayIterator( $this->items );
44
	}
45
46
	/**
47
	 * Add item
48
	 *
49
	 * @param Item $item Item.
50
	 */
51 2
	public function add_item( Item $item ) {
52 2
		$this->items[] = $item;
53 2
	}
54
55
	/**
56
	 * Calculate the total amount of all items
57
	 */
58 2
	public function get_amount() {
59 2
		$amount = 0;
60
61 2
		$use_bcmath = extension_loaded( 'bcmath' );
62
63 2
		foreach ( $this->items as $item ) {
64 2
			if ( $use_bcmath ) {
65
				// Use non-locale aware float value.
66
				// @link http://php.net/sprintf.
67 2
				$item_amount = sprintf( '%F', $item->get_amount() );
68
69 2
				$amount = bcadd( $amount, $item_amount, 8 );
70
			} else {
71
				$amount += $item->get_amount();
72
			}
73
		}
74
75 2
		return new Money( $amount );
76
	}
77
}
78