Completed
Pull Request — master (#1)
by Daniel
10:00
created

PhpcrOdmAgent   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 30.36 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 34
loc 112
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A find() 0 13 3
A save() 0 5 1
A delete() 0 5 1
A getIdentifier() 18 18 2
A setParent() 16 16 2
A supports() 0 13 2
A query() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm;
6
7
use Psi\Component\ObjectAgent\AgentInterface;
8
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
9
use Doctrine\Common\Util\ClassUtils;
10
use Psi\Component\ObjectAgent\Agent\Doctrine\Event\PhpcrOdmObjectEvent;
11
use Psi\Component\ObjectAgent\Agent\Doctrine\Event\ObjectEvent;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Psi\Component\ObjectAgent\Events;
14
use Psi\Component\ObjectAgent\Query\Query;
15
use Psi\Component\ObjectAgent\Exception\ObjectNotFoundException;
16
use Doctrine\Common\Persistence\Mapping\MappingException;
17
18
class PhpcrOdmAgent implements AgentInterface
19
{
20
    private $documentManager;
21
22
    public function __construct(
23
        DocumentManagerInterface $documentManager
24
    )
25
    {
26
        $this->documentManager = $documentManager;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function find($identifier, string $class = null)
33
    {
34
        $object = $this->documentManager->find($class, $identifier);
35
36
        if (null === $object) {
37
            throw new ObjectNotFoundException(sprintf(
38
                'Could not find document with identifier "%s" (class "%s")',
39
                $identifier, null === $class ? '<null>' : $class
40
            ));
41
        }
42
43
        return $object;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function save($object)
50
    {
51
        $this->documentManager->persist($object);
52
        $this->documentManager->flush();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function delete($object)
59
    {
60
        $this->documentManager->remove($object);
61
        $this->documentManager->flush();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 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...
68
    {
69
        $objectFqn = ClassUtils::getRealClass(get_class($object));
70
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
71
        $uuidFieldName = $metadata->getUuidFieldName();
72
73
        if (!$uuidFieldName) {
74
            throw new \RuntimeException(sprintf(
75
                'Document "%s" does not have a UUID-mapped property. All '.
76
                'PHPCR-ODM documents must have a mapped UUID proprety.',
77
                $objectFqn
78
            ));
79
        }
80
81
        $node = $this->documentManager->getNodeForDocument($object);
82
83
        return $node->getIdentifier();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 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...
90
    {
91
        $objectFqn = ClassUtils::getRealClass(get_class($object));
92
        $metadata = $this->documentManager->getClassMetadata($objectFqn);
93
        $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...
94
95
        if (!$parentField) {
96
            throw new \RuntimeException(sprintf(
97
                'Document "%s" does not have a ParentDocument mapping All '.
98
                'PHPCR-ODM documents must have a mapped parent proprety.',
99
                $objectFqn
100
            ));
101
        }
102
103
        $metadata->setFieldValue($object, $parentField, $parent);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function supports(string $class): bool
110
    {
111
        $metadataFactory = $this->documentManager->getMetadataFactory();
112
113
        $supports = false;
114
        try {
115
            $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class));
116
            $supports = true;
117
        } catch (MappingException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
118
        }
119
120
        return $supports;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function query(Query $query): \Traversable
127
    {
128
    }
129
}
130