Completed
Push — master ( bd48a4...4fe654 )
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\Exception\ObjectNotFoundException;
12
use Psi\Component\ObjectAgent\Query\Query;
13
14
class PhpcrOdmAgent implements AgentInterface
15
{
16
    private $documentManager;
17
18
    public function __construct(
19
        DocumentManagerInterface $documentManager
20
    ) {
21
        $this->documentManager = $documentManager;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function find($identifier, string $class = null)
28
    {
29
        $object = $this->documentManager->find($class, $identifier);
30
31
        if (null === $object) {
32
            throw ObjectNotFoundException::forClassAndIdentifier($class, $identifier);
33
        }
34
35
        return $object;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function save($object)
42
    {
43
        $this->documentManager->persist($object);
44
        $this->documentManager->flush();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function delete($object)
51
    {
52
        $this->documentManager->remove($object);
53
        $this->documentManager->flush();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 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...
60
    {
61
        $objectFqn = ClassUtils::getRealClass(get_class($object));
62
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
63
        $uuidFieldName = $metadata->getUuidFieldName();
64
65
        if (!$uuidFieldName) {
66
            throw new \RuntimeException(sprintf(
67
                'Document "%s" does not have a UUID-mapped property. All '.
68
                'PHPCR-ODM documents must have a mapped UUID proprety.',
69
                $objectFqn
70
            ));
71
        }
72
73
        $node = $this->documentManager->getNodeForDocument($object);
74
75
        return $node->getIdentifier();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 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...
82
    {
83
        $objectFqn = ClassUtils::getRealClass(get_class($object));
84
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
85
        $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...
86
87
        if (!$parentField) {
88
            throw new \RuntimeException(sprintf(
89
                'Document "%s" does not have a ParentDocument mapping All '.
90
                'PHPCR-ODM documents must have a mapped parent proprety.',
91
                $objectFqn
92
            ));
93
        }
94
95
        $metadata->setFieldValue($object, $parentField, $parent);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 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...
102
    {
103
        $metadataFactory = $this->documentManager->getMetadataFactory();
104
105
        $supports = false;
106
        try {
107
            $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class));
108
            $supports = true;
109
        } catch (MappingException $exception) {
110
            // no metadata - class is not known to phpcr-odm
111
        }
112
113
        return $supports;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 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...
120
    {
121
        $sourceAlias = 'a';
122
        $queryBuilder = $this->documentManager->getRepository($query->getClassFqn())->createQueryBuilder($sourceAlias);
123
124
        if ($query->hasExpression()) {
125
            $visitor = new ExpressionVisitor(
126
                $queryBuilder,
127
                $sourceAlias
128
            );
129
130
            $visitor->dispatch($query->getExpression());
131
        }
132
133
        $orderBy = $queryBuilder->orderBy();
134
        foreach ($query->getOrderings() as $field => $order) {
135
            $order = strtolower($order);
136
            $orderBy->{$order}()->field($sourceAlias . '.' .  $field);
137
        }
138
139
        if (null !== $query->getFirstResult()) {
140
            $queryBuilder->setFirstResult($query->getFirstResult());
141
        }
142
143
        if (null !== $query->getMaxResults()) {
144
            $queryBuilder->setMaxResults($query->getMaxResults());
145
        }
146
147
        return $queryBuilder->getQuery()->execute();
148
    }
149
150
    /**
151
     * Return the document mangaer instance (for use in events).
152
     */
153
    public function getDocumentManager(): DocumentManagerInterface
154
    {
155
        return $this->documentManager;
156
    }
157
}
158