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

CollectionRepository::persist()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 1
crap 3
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