CollectionRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 65%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 87
ccs 26
cts 40
cp 0.65
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A delete() 0 5 1
A deleteBy() 0 11 2
A countBy() 0 6 1
A find() 0 8 1
A findAll() 0 4 1
A findBy() 0 6 1
A findOneBy() 0 6 1
A persist() 0 9 3
A getPersisted() 0 4 1
1
<?php
2
3
namespace PolderKnowledge\EntityService\Repository\Doctrine;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\Common\Collections\Criteria;
8
use PolderKnowledge\EntityService\Repository\EntityRepositoryInterface;
9
use PolderKnowledge\EntityService\Repository\Feature\DeletableInterface;
10
use PolderKnowledge\EntityService\Repository\Feature\ReadableInterface;
11
use PolderKnowledge\EntityService\Repository\Feature\TransactionAwareInterface;
12
use PolderKnowledge\EntityService\Repository\Feature\WritableInterface;
13
use PolderKnowledge\EntityService\Repository\Util;
14
use UnexpectedValueException;
15
16
class CollectionRepository implements
17
    EntityRepositoryInterface,
18
    DeletableInterface,
19
    ReadableInterface,
20
    WritableInterface
21
{
22
    private $allItems;
23
24
    private $persisted;
25
26 18
    public function __construct(array $initialItems = [])
27
    {
28 18
        $this->allItems = new ArrayCollection($initialItems);
29 18
        $this->persisted = new ArrayCollection;
30 18
    }
31
32
    public function delete($entity)
33
    {
34
        $this->allItems->removeElement($entity);
35
        $this->persisted->removeElement($entity);
36
    }
37
38
    public function deleteBy($criteria)
39
    {
40
        $criteria = Util::normalizeCriteria($criteria);
41
42
        $itemsToDelete = $this->allItems->matching($criteria);
43
44
        foreach ($itemsToDelete as $item) {
45
            $this->allItems->removeElement($item);
46
            $this->persisted->removeElement($item);
47
        }
48
    }
49
50 6
    public function countBy($criteria)
51
    {
52 6
        $criteria = Util::normalizeCriteria($criteria);
53
54 6
        return $this->allItems->matching($criteria)->count();
55
    }
56
57 3
    public function find($id)
58
    {
59 3
        return $this->allItems->filter(
60 3
            function ($entity) use ($id) {
61 3
                return $entity->getId() === $id;
62 3
            }
63 3
        )->first();
64
    }
65
66 9
    public function findAll()
67
    {
68 9
        return $this->allItems->toArray();
69
    }
70
71 3
    public function findBy($criteria)
72
    {
73 3
        $criteria = Util::normalizeCriteria($criteria);
74
75 3
        return $this->allItems->matching($criteria)->toArray();
76
    }
77
78
    public function findOneBy($criteria)
79
    {
80
        $criteria = Util::normalizeCriteria($criteria);
81
82
        return $this->allItems->matching($criteria)->first();
83
    }
84
85 12
    public function persist($entity)
86
    {
87 12
        if (!$this->allItems->contains($entity)) {
88 9
            $this->allItems->add($entity);
89
        }
90 12
        if (!$this->persisted->contains($entity)) {
91 12
            $this->persisted->add($entity);
92
        }
93 12
    }
94
95
    /**
96
     * Method outside of the standard interface, for testing purposes
97
     */
98 6
    public function getPersisted(): ArrayCollection
99
    {
100 6
        return clone $this->persisted;
101
    }
102
}
103