Completed
Pull Request — master (#7)
by
unknown
01:41
created

CollectionRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 79
ccs 22
cts 36
cp 0.6111
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 9 2
A countBy() 0 4 1
A find() 0 8 1
A findAll() 0 4 1
A findBy() 0 4 1
A findOneBy() 0 4 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 UnexpectedValueException;
14
15
class CollectionRepository implements
16
    EntityRepositoryInterface,
17
    DeletableInterface,
18
    ReadableInterface,
19
    WritableInterface
20
{
21
    private $allItems;
22
23
    private $persisted;
24
25 15
    public function __construct(array $initialItems = [])
26
    {
27 15
        $this->allItems = new ArrayCollection($initialItems);
28 15
        $this->persisted = new ArrayCollection;
29 15
    }
30
31
    public function delete($entity)
32
    {
33
        $this->allItems->removeElement($entity);
34
        $this->persisted->removeElement($entity);
35
    }
36
37
    public function deleteBy($criteria)
38
    {
39
        $itemsToDelete = $this->allItems->matching($criteria);
0 ignored issues
show
Bug introduced by
It seems like $criteria defined by parameter $criteria on line 37 can also be of type array; however, Doctrine\Common\Collecti...yCollection::matching() does only seem to accept object<Doctrine\Common\Collections\Criteria>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
41
        foreach ($itemsToDelete as $item) {
42
            $this->allItems->removeElement($item);
43
            $this->persisted->removeElement($item);
44
        }
45
    }
46
47 6
    public function countBy($criteria)
48
    {
49 6
        return $this->allItems->matching($criteria)->count();
0 ignored issues
show
Bug introduced by
It seems like $criteria defined by parameter $criteria on line 47 can also be of type array; however, Doctrine\Common\Collecti...yCollection::matching() does only seem to accept object<Doctrine\Common\Collections\Criteria>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
    }
51
52 3
    public function find($id)
53
    {
54 3
        return $this->allItems->filter(
55 3
            function ($entity) use ($id) {
56 3
                return $entity->getId() === $id;
57 3
            }
58 3
        )->first();
59
    }
60
61 9
    public function findAll()
62
    {
63 9
        return $this->allItems->toArray();
64
    }
65
66
    public function findBy($criteria)
67
    {
68
        return $this->allItems->matching($criteria)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $criteria defined by parameter $criteria on line 66 can also be of type array; however, Doctrine\Common\Collecti...yCollection::matching() does only seem to accept object<Doctrine\Common\Collections\Criteria>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
69
    }
70
71
    public function findOneBy($criteria)
72
    {
73
        return $this->allItems->matching($criteria)->first();
0 ignored issues
show
Bug introduced by
It seems like $criteria defined by parameter $criteria on line 71 can also be of type array; however, Doctrine\Common\Collecti...yCollection::matching() does only seem to accept object<Doctrine\Common\Collections\Criteria>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
    }
75
76 12
    public function persist($entity)
77
    {
78 12
        if (!$this->allItems->contains($entity)) {
79 9
            $this->allItems->add($entity);
80
        }
81 12
        if (!$this->persisted->contains($entity)) {
82 12
            $this->persisted->add($entity);
83
        }
84 12
    }
85
86
    /**
87
     * Method outside of the standard interface, for testing purposes
88
     */
89 6
    public function getPersisted(): ArrayCollection
90
    {
91 6
        return clone $this->persisted;
92
    }
93
}
94