Completed
Push — master ( 4fe654...d2edea )
by Daniel
04:44 queued 02:31
created

PhpcrOdmAgent::getCapabilities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
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\ObjectNotFoundException;
13
use Psi\Component\ObjectAgent\Query\Comparison;
14
use Psi\Component\ObjectAgent\Query\Query;
15
16
class PhpcrOdmAgent implements AgentInterface
17
{
18
    private $documentManager;
19
20
    public function __construct(
21
        DocumentManagerInterface $documentManager
22
    ) {
23
        $this->documentManager = $documentManager;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function find($identifier, string $class = null)
30
    {
31
        $object = $this->documentManager->find($class, $identifier);
32
33
        if (null === $object) {
34
            throw ObjectNotFoundException::forClassAndIdentifier($class, $identifier);
35
        }
36
37
        return $object;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 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...
44
    {
45
        return Capabilities::create([
46
            'can_set_parent' => true,
47
            'supported_comparators' => [
48
                Comparison::EQUALS,
49
                Comparison::NOT_EQUALS,
50
                Comparison::LESS_THAN,
51
                Comparison::LESS_THAN_EQUAL,
52
                Comparison::GREATER_THAN,
53
                Comparison::GREATER_THAN_EQUAL,
54
                Comparison::IN,
55
                Comparison::NOT_IN,
56
                Comparison::CONTAINS,
57
                Comparison::NOT_CONTAINS,
58
                Comparison::NULL,
59
                Comparison::NOT_NULL,
60
            ],
61
        ]);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function save($object)
68
    {
69
        $this->documentManager->persist($object);
70
        $this->documentManager->flush();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function delete($object)
77
    {
78
        $this->documentManager->remove($object);
79
        $this->documentManager->flush();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 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...
86
    {
87
        $objectFqn = ClassUtils::getRealClass(get_class($object));
88
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
89
        $uuidFieldName = $metadata->getUuidFieldName();
90
91
        if (!$uuidFieldName) {
92
            throw new \RuntimeException(sprintf(
93
                'Document "%s" does not have a UUID-mapped property. All '.
94
                'PHPCR-ODM documents must have a mapped UUID proprety.',
95
                $objectFqn
96
            ));
97
        }
98
99
        $node = $this->documentManager->getNodeForDocument($object);
100
101
        return $node->getIdentifier();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 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...
108
    {
109
        $objectFqn = ClassUtils::getRealClass(get_class($object));
110
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
111
        $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...
112
113
        if (!$parentField) {
114
            throw new \RuntimeException(sprintf(
115
                'Document "%s" does not have a ParentDocument mapping All '.
116
                'PHPCR-ODM documents must have a mapped parent proprety.',
117
                $objectFqn
118
            ));
119
        }
120
121
        $metadata->setFieldValue($object, $parentField, $parent);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 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...
128
    {
129
        $metadataFactory = $this->documentManager->getMetadataFactory();
130
131
        $supports = false;
132
        try {
133
            $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class));
134
            $supports = true;
135
        } catch (MappingException $exception) {
136
            // no metadata - class is not known to phpcr-odm
137
        }
138
139
        return $supports;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 View Code Duplication
    public function query(Query $query): \Traversable
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...
146
    {
147
        $sourceAlias = 'a';
148
        $queryBuilder = $this->documentManager->getRepository($query->getClassFqn())->createQueryBuilder($sourceAlias);
149
150
        if ($query->hasExpression()) {
151
            $visitor = new ExpressionVisitor(
152
                $queryBuilder,
153
                $sourceAlias
154
            );
155
156
            $visitor->dispatch($query->getExpression());
157
        }
158
159
        $orderBy = $queryBuilder->orderBy();
160
        foreach ($query->getOrderings() as $field => $order) {
161
            $order = strtolower($order);
162
            $orderBy->{$order}()->field($sourceAlias . '.' .  $field);
163
        }
164
165
        if (null !== $query->getFirstResult()) {
166
            $queryBuilder->setFirstResult($query->getFirstResult());
167
        }
168
169
        if (null !== $query->getMaxResults()) {
170
            $queryBuilder->setMaxResults($query->getMaxResults());
171
        }
172
173
        return $queryBuilder->getQuery()->execute();
174
    }
175
176
    /**
177
     * Return the document mangaer instance (for use in events).
178
     */
179
    public function getDocumentManager(): DocumentManagerInterface
180
    {
181
        return $this->documentManager;
182
    }
183
}
184