Completed
Pull Request — master (#6)
by Daniel
08:59
created

CollectionsAgent::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace Psi\Bridge\ObjectAgent\Doctrine\Collections;
4
5
use Psi\Component\ObjectAgent\AgentInterface;
6
use Psi\Component\ObjectAgent\Exception\ObjectNotFoundException;
7
use Psi\Component\ObjectAgent\Query\Query;
8
use Psi\Component\ObjectAgent\Exception\BadMethodCallException;
9
use Doctrine\Common\Collections\Criteria;
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
    /**
51
     * {@inheritdoc}
52
     */
53
    public function save($object)
54
    {
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function delete($object)
61
    {
62
        $this->store->delete($object);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function query(Query $query): \Traversable
69
    {
70
        $collection = $this->store->getCollection($query->getClassFqn());
71
        $expressionBuilder = Criteria::expr();
72
        $visitor = new CollectionsVisitor($expressionBuilder);
73
        $doctrineExpression = $visitor->dispatch($query->getExpression());
74
        $criteria = new Criteria($doctrineExpression);
75
76
        return $collection->matching($criteria);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getIdentifier($object)
83
    {
84
        foreach ($this->store->getCollection(get_class($object)) as $identifier => $element) {
85
            if ($element === $object)
86
                return $identifier;
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