Passed
Push — master ( 966fd5...e75d59 )
by Pavel
03:58
created

LazyCriteriaCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Proxy;
4
5
use Doctrine\Common\Collections\AbstractLazyCollection;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\Common\Collections\Criteria;
9
use Doctrine\Common\Collections\Selectable;
10
11
final class LazyCriteriaCollection extends AbstractLazyCollection
12
{
13
    /**
14
     * @var Selectable
15
     */
16
    private $matcher;
17
    /**
18
     * @var Criteria
19
     */
20
    private $criteria;
21
    /**
22
     * @var Collection
23
     */
24
    private $prefetched;
25
26
    /**
27
     * LazyCriteriaCollection constructor.
28
     *
29
     * @param Selectable $matcher
30
     * @param Criteria   $criteria
31
     * @param Collection $prefetched
32
     */
33 1
    public function __construct(Selectable $matcher, Criteria $criteria, Collection $prefetched = null)
34
    {
35 1
        $this->matcher    = $matcher;
36 1
        $this->criteria   = $criteria;
37 1
        $this->prefetched = $prefetched ?: new ArrayCollection();
38 1
    }
39
40
    /**
41
     * Do the initialization logic
42
     *
43
     * @return void
44
     */
45 1
    protected function doInitialize()
46
    {
47 1
        $this->collection = new ArrayCollection();
48
49 1
        foreach ($this->prefetched as $element) {
50
            $this->collection->add($element);
51 1
        }
52
53 1
        foreach ($this->matcher->matching($this->criteria) as $element) {
54 1
            if (!$this->collection->contains($element)) {
55 1
                $this->collection->add($element);
56 1
            }
57 1
        }
58
59 1
        $this->collection = $this->collection->matching($this->criteria);
60 1
    }
61
}
62