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

CollectionsAgent::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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