Completed
Push — master ( d1b735...0bfc48 )
by Daniel
08:29
created

PhpcrOdmAgent::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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) {
113
            // no metadata - class is not known to phpcr-odm
114
        }
115
116
        return $supports;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function query(Query $query): \Traversable
123
    {
124
        $sourceAlias = 'a';
125
        $queryBuilder = $this->documentManager->getRepository($query->getClassFqn())->createQueryBuilder($sourceAlias);
126
        $visitor = new ExpressionVisitor(
127
            $queryBuilder,
128
            $sourceAlias
129
        );
130
131
        $visitor->dispatch($query->getExpression());
132
133
        return $queryBuilder->getQuery()->execute();
134
    }
135
}
136