SalesmanFilterIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A accept() 0 18 3
1
<?php
2
namespace Germania\Salesmen;
3
4
class SalesmanFilterIterator extends \FilterIterator
5
{
6
7
    /**
8
     * @var mixed
9
     */
10
    public $salesman_filter;
11
12
13
    /**
14
     * @param \Traversable $collection      Collection of SalesmanIdProviderInterface
15
     * @param SalesmanIdProviderInterface|SalesmanIdProviderInterface[]|int    $salesman_filter The Salesman ID to filter for
16
     */
17 180
    public function __construct( \Traversable $collection, $salesman_filter )
18
    {
19
        // Allow for getSalesmanId
20 180
        $this->salesman_filter = ($salesman_filter instanceOf SalesmanIdProviderInterface)
21 60
        ? $salesman_filter->getSalesmanId()
22 120
        : $salesman_filter;
23
24
        // Cast to array
25 180
        if (!is_array($this->salesman_filter)) {
26 96
            $this->salesman_filter = array( $this->salesman_filter );
27
        }
28
29
        // Take care of Traversable's two faces,
30
        // since both IteratorAggregate and Iterator implement Traversable
31 180
        parent::__construct( $collection instanceOf \IteratorAggregate ? $collection->getIterator() : $collection );
32
33 180
    }
34
35
36 180
    public function accept()
37
    {
38 180
        $item = $this->getInnerIterator()->current();
39
40
        // Disclose items not implementing SalesmanIdProviderInterface
41 180
        if (!$item instanceOf SalesmanIdProviderInterface) {
42 60
            return false;
43
        }
44
45
        // Cast to array
46 120
        $item_salesman_id = $item->getSalesmanId();
47 120
        if (!is_array($item_salesman_id)) {
48 120
            $item_salesman_id = array( $item_salesman_id );
49
        }
50
51
        // Check intersection
52 120
        return !empty(array_intersect($this->salesman_filter, $item_salesman_id));
53
    }
54
}
55