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

LazyCriteriaCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 51
ccs 16
cts 17
cp 0.9412
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A doInitialize() 0 16 4
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