Completed
Pull Request — master (#1)
by Daniel
06:19
created

PhpcrOdmAgent::find()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 2
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 new ObjectNotFoundException(sprintf(
33
                'Could not find document with identifier "%s" (class "%s")',
34
                $identifier, null === $class ? '<null>' : $class
35
            ));
36
        }
37
38
        return $object;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function save($object)
45
    {
46
        $this->documentManager->persist($object);
47
        $this->documentManager->flush();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function delete($object)
54
    {
55
        $this->documentManager->remove($object);
56
        $this->documentManager->flush();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 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...
63
    {
64
        $objectFqn = ClassUtils::getRealClass(get_class($object));
65
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
66
        $uuidFieldName = $metadata->getUuidFieldName();
67
68
        if (!$uuidFieldName) {
69
            throw new \RuntimeException(sprintf(
70
                'Document "%s" does not have a UUID-mapped property. All '.
71
                'PHPCR-ODM documents must have a mapped UUID proprety.',
72
                $objectFqn
73
            ));
74
        }
75
76
        $node = $this->documentManager->getNodeForDocument($object);
77
78
        return $node->getIdentifier();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 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...
85
    {
86
        $objectFqn = ClassUtils::getRealClass(get_class($object));
87
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
88
        $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...
89
90
        if (!$parentField) {
91
            throw new \RuntimeException(sprintf(
92
                'Document "%s" does not have a ParentDocument mapping All '.
93
                'PHPCR-ODM documents must have a mapped parent proprety.',
94
                $objectFqn
95
            ));
96
        }
97
98
        $metadata->setFieldValue($object, $parentField, $parent);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function supports(string $class): bool
105
    {
106
        $metadataFactory = $this->documentManager->getMetadataFactory();
107
108
        $supports = false;
109
        try {
110
            $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class));
111
            $supports = true;
112
        } catch (MappingException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
113
        }
114
115
        return $supports;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function query(Query $query): \Traversable
122
    {
123
        $sourceAlias = 'a';
124
        $queryBuilder = $this->documentManager->getRepository($query->getClassFqn())->createQueryBuilder($sourceAlias);
125
        $visitor = new ExpressionVisitor(
126
            $queryBuilder,
127
            $sourceAlias
128
        );
129
130
        $visitor->dispatch($query->getExpression());
131
132
        return $queryBuilder->getQuery()->execute();
133
    }
134
}
135