Completed
Pull Request — master (#6)
by Daniel
09:09 queued 07:03
created

CollectionsAgent::setParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Psi\Bridge\ObjectAgent\Doctrine\Collections;
4
5
use Doctrine\Common\Collections\Criteria;
6
use Psi\Component\ObjectAgent\AgentInterface;
7
use Psi\Component\ObjectAgent\Exception\BadMethodCallException;
8
use Psi\Component\ObjectAgent\Exception\ObjectNotFoundException;
9
use Psi\Component\ObjectAgent\Query\Query;
10
11
class CollectionsAgent implements AgentInterface
12
{
13
    private $store;
14
15
    public function __construct(Store $store)
16
    {
17
        $this->store = $store;
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function supports(string $classFqn): bool
24
    {
25
        return $this->store->hasCollection($classFqn);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function find($identifier, string $classFqn = null)
32
    {
33
        if (null === $classFqn) {
34
            throw BadMethodCallException::classArgumentIsMandatory(__CLASS__);
35
        }
36
37
        $object = $this->store->find($classFqn, $identifier);
38
39
        if (null === $object) {
40
            throw new ObjectNotFoundException(sprintf(
41
                'Could not find object of class "%s" with identifier "%s"',
42
                $classFqn, $identifier
43
            ));
44
        }
45
46
        return $object;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function save($object)
53
    {
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function delete($object)
60
    {
61
        $this->store->delete($object);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function query(Query $query): \Traversable
68
    {
69
        $collection = $this->store->getCollection($query->getClassFqn());
70
        $expressionBuilder = Criteria::expr();
71
        $visitor = new CollectionsVisitor($expressionBuilder);
72
        $doctrineExpression = $visitor->dispatch($query->getExpression());
73
        $criteria = new Criteria($doctrineExpression);
74
75
        return $collection->matching($criteria);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getIdentifier($object)
82
    {
83
        foreach ($this->store->getCollection(get_class($object)) as $identifier => $element) {
84
            if ($element === $object) {
85
                return $identifier;
86
            }
87
        }
88
89
        throw new \RuntimeException(sprintf(
90
            'Could not find identifier for object of class "%s"',
91
            get_class($object)
92
        ));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function setParent($object, $parent)
99
    {
100
        throw BadMethodCallException::setParentNotSupported(__CLASS__);
101
    }
102
}
103