ItemBag::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Cart Item Bag
4
 */
5
6
namespace Omnipay\Common;
7
8
/**
9
 * Cart Item Bag
10
 *
11
 * This class defines a bag (multi element set or array) of single cart items
12
 * in the Omnipay system.
13
 *
14
 */
15
class ItemBag implements \IteratorAggregate, \Countable
16
{
17
    /**
18
     * Item storage
19
     *
20
     *
21
     * @var array
22
     */
23
    protected $items;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param array $items An array of items
29
     */
30 27
    public function __construct(array $items = array())
31
    {
32 27
        $this->replace($items);
33 27
    }
34
35
    /**
36
     * Return all the items
37
     *
38
     *
39
     * @return array An array of items
40
     */
41 15
    public function all()
42
    {
43 15
        return $this->items;
44
    }
45
46
    /**
47
     * Replace the contents of this bag with the specified items
48
     *
49
     *
50
     * @param array $items An array of items
51
     */
52 27
    public function replace(array $items = array())
53
    {
54 27
        $this->items = array();
55
56 27
        foreach ($items as $item) {
57 12
            $this->add($item);
58
        }
59 27
    }
60
61
    /**
62
     * Add an item to the bag
63
     *
64
     *
65
     * @param ItemInterface|array $item An existing item, or associative array of item parameters
66
     */
67 27
    public function add($item)
68
    {
69 27
        if ($item instanceof ItemInterface) {
70 15
            $this->items[] = $item;
71
        } else {
72 12
            $this->items[] = new Item($item);
73
        }
74 27
    }
75
76
    /**
77
     * Returns an iterator for items
78
     *
79
     * @return \ArrayIterator An \ArrayIterator instance
80
     */
81 3
    public function getIterator(): \Traversable
82
    {
83 3
        return new \ArrayIterator($this->items);
84
    }
85
86
    /**
87
     * Returns the number of items
88
     *
89
     * @return int The number of items
90
     */
91 6
    public function count(): int
92
    {
93 6
        return count($this->items);
94
    }
95
}
96