Completed
Push — master ( 5d85cb...99533f )
by Pavel
03:42
created

CollectionSource::withCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bankiru\Seo\Integration\Local;
4
5
use Bankiru\Seo\ConditionInterface;
6
use Bankiru\Seo\SourceInterface;
7
use Doctrine\Common\Collections\Collection;
8
9
final class CollectionSource implements \IteratorAggregate, SourceInterface
10
{
11
    /** @var  Collection */
12
    private $collection;
13
    /** @var  ConditionInterface[] */
14
    private $conditions = [];
15
16
    /**
17
     * CollectionSource constructor.
18
     *
19
     * @param Collection $collection
20
     */
21 6
    public function __construct(Collection $collection)
22
    {
23 6
        $this->collection = $collection;
24 6
    }
25
26
    /** {@inheritdoc} */
27 1
    public function count()
28
    {
29 1
        return iterator_count($this->createFilterIterator());
30
    }
31
32
    /** {@inheritdoc} */
33 2
    public function withCondition(ConditionInterface $condition)
34
    {
35 2
        $this->conditions[] = $condition;
36 2
    }
37
38
    /** {@inheritdoc} */
39 6
    public function getIterator()
40
    {
41 6
        return $this->createFilterIterator();
42
    }
43
44
    /**
45
     * @return \CallbackFilterIterator
46
     */
47 6
    private function createFilterIterator()
48
    {
49 6
        return new \CallbackFilterIterator(
50 6
            new \IteratorIterator($this->collection->getIterator()),
51 6
            function ($element) {
52 6
                foreach ($this->conditions as $condition) {
53 2
                    if (!$condition->match($element)) {
54 1
                        return false;
55
                    }
56 6
                }
57
58 6
                return true;
59
            }
60 6
        );
61
    }
62
}
63