Completed
Push — master ( 390a97...c4c478 )
by Daniel
9s
created

PhpcrOdmAgent::getDocumentManager()   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 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm;
6
7
use Doctrine\Common\Persistence\Mapping\MappingException;
8
use Doctrine\Common\Util\ClassUtils;
9
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
10
use Psi\Component\ObjectAgent\AgentInterface;
11
use Psi\Component\ObjectAgent\Capabilities;
12
use Psi\Component\ObjectAgent\Exception\BadMethodCallException;
13
use Psi\Component\ObjectAgent\Exception\ObjectNotFoundException;
14
use Psi\Component\ObjectAgent\Query\Comparison;
15
use Psi\Component\ObjectAgent\Query\Query;
16
17
class PhpcrOdmAgent implements AgentInterface
18
{
19
    private $documentManager;
20
21
    public function __construct(
22
        DocumentManagerInterface $documentManager
23
    ) {
24
        $this->documentManager = $documentManager;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function find($identifier, string $class = null)
31
    {
32
        $object = $this->documentManager->find($class, $identifier);
33
34
        if (null === $object) {
35
            throw ObjectNotFoundException::forClassAndIdentifier($class, $identifier);
36
        }
37
38
        return $object;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 View Code Duplication
    public function getCapabilities(): Capabilities
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        return Capabilities::create([
47
            'can_set_parent' => true,
48
            'supported_comparators' => [
49
                Comparison::EQUALS,
50
                Comparison::NOT_EQUALS,
51
                Comparison::LESS_THAN,
52
                Comparison::LESS_THAN_EQUAL,
53
                Comparison::GREATER_THAN,
54
                Comparison::GREATER_THAN_EQUAL,
55
                Comparison::IN,
56
                Comparison::NOT_IN,
57
                Comparison::CONTAINS,
58
                Comparison::NOT_CONTAINS,
59
                Comparison::NULL,
60
                Comparison::NOT_NULL,
61
            ],
62
        ]);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function save($object)
69
    {
70
        $this->documentManager->persist($object);
71
        $this->documentManager->flush();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function delete($object)
78
    {
79
        $this->documentManager->remove($object);
80
        $this->documentManager->flush();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 View Code Duplication
    public function getIdentifier($object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $objectFqn = ClassUtils::getRealClass(get_class($object));
89
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
90
        $uuidFieldName = $metadata->getUuidFieldName();
91
92
        if (!$uuidFieldName) {
93
            throw new \RuntimeException(sprintf(
94
                'Document "%s" does not have a UUID-mapped property. All '.
95
                'PHPCR-ODM documents must have a mapped UUID proprety.',
96
                $objectFqn
97
            ));
98
        }
99
100
        $node = $this->documentManager->getNodeForDocument($object);
101
102
        return $node->getIdentifier();
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 View Code Duplication
    public function setParent($object, $parent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $objectFqn = ClassUtils::getRealClass(get_class($object));
111
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
112
        $parentField = $metadata->parentMapping;
0 ignored issues
show
Bug introduced by
Accessing parentMapping on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
113
114
        if (!$parentField) {
115
            throw new \RuntimeException(sprintf(
116
                'Document "%s" does not have a ParentDocument mapping All '.
117
                'PHPCR-ODM documents must have a mapped parent proprety.',
118
                $objectFqn
119
            ));
120
        }
121
122
        $metadata->setFieldValue($object, $parentField, $parent);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 View Code Duplication
    public function supports(string $class): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $metadataFactory = $this->documentManager->getMetadataFactory();
131
132
        $supports = false;
133
        try {
134
            $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class));
135
            $supports = true;
136
        } catch (MappingException $exception) {
137
            // no metadata - class is not known to phpcr-odm
138
        }
139
140
        return $supports;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function query(Query $query): \Traversable
147
    {
148
        $sourceAlias = 'a';
149
        $queryBuilder = $this->documentManager->getRepository($query->getClassFqn())->createQueryBuilder($sourceAlias);
150
151
        if ($query->hasExpression()) {
152
            $visitor = new ExpressionVisitor(
153
                $queryBuilder,
154
                $sourceAlias
155
            );
156
157
            $visitor->dispatch($query->getExpression());
158
        }
159
160
        $orderBy = $queryBuilder->orderBy();
161
        foreach ($query->getOrderings() as $field => $order) {
162
            $order = strtolower($order);
163
            $orderBy->{$order}()->field($sourceAlias . '.' .  $field);
164
        }
165
166
        if (null !== $query->getFirstResult()) {
167
            $queryBuilder->setFirstResult($query->getFirstResult());
168
        }
169
170
        if (null !== $query->getMaxResults()) {
171
            $queryBuilder->setMaxResults($query->getMaxResults());
172
        }
173
174
        return $queryBuilder->getQuery()->execute();
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function queryCount(Query $query): int
181
    {
182
        throw BadMethodCallException::queryCountNotSupported(__CLASS__);
183
    }
184
185
    /**
186
     * Return the document mangaer instance (for use in events).
187
     */
188
    public function getDocumentManager(): DocumentManagerInterface
189
    {
190
        return $this->documentManager;
191
    }
192
}
193