PhpcrOdmEnhancer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 9
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B enhanceFromClass() 0 26 5
A enhanceFromObject() 0 5 1
A supports() 0 4 1
1
<?php
2
3
namespace Psi\Component\Description\Enhancer\Doctrine;
4
5
use Doctrine\ODM\PHPCR\Mapping\ClassMetadataFactory;
6
use Doctrine\Common\Util\ClassUtils;
7
use Psi\Component\Description\EnhancerInterface;
8
use Psi\Component\Description\DescriptionInterface;
9
use Psi\Component\Description\Subject;
10
use Psi\Component\Description\Descriptor\BooleanDescriptor;
11
use Psi\Component\Description\Descriptor\ArrayDescriptor;
12
use Psi\Component\Description\Descriptor\ClassDescriptor;
13
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
14
use Psi\Component\Description\Descriptor\StringDescriptor;
15
16
class PhpcrOdmEnhancer implements EnhancerInterface
17
{
18
    private $documentManager;
19
20
    public function __construct(DocumentManagerInterface $documentManager)
21
    {
22
        $this->documentManager = $documentManager;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function enhanceFromClass(DescriptionInterface $description, \ReflectionClass $class)
29
    {
30
        $metadataFactory = $this->documentManager->getMetadataFactory();
31
        $metadata = $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class->getName()));
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
32
        $childClasses = $metadata->getChildClasses();
33
34
        // cast child classes to strings because of https://github.com/doctrine/phpcr-odm/issues/723
35
        $childClasses = array_map(function ($value) {
36
            return (string) $value;
37
        }, $childClasses);
38
39
        $childTypes = [];
40
        // explode the allowed types into concrete classes
41
        foreach ($metadataFactory->getAllMetadata() as $childMetadata) {
42
            foreach ($childClasses as $childClass) {
43
                $childRefl = $childMetadata->getReflectionClass();
44
                if ($childClass == $childRefl->getName() || $childRefl->isSubclassOf($childClass)) {
45
                    $childTypes[] = $childRefl->getName();
46
                }
47
            }
48
        }
49
50
        $description->set('hierarchy.allow_children', new BooleanDescriptor(!$metadata->isLeaf()));
51
        $description->set('hierarchy.children_types', new ArrayDescriptor($childTypes));
52
        $description->set('std.class', new ClassDescriptor(new \ReflectionClass(ClassUtils::getRealClass($class->getName()))));
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function enhanceFromObject(DescriptionInterface $description, Subject $subject)
59
    {
60
        $node = $this->documentManager->getNodeForDocument($subject->getObject());
61
        $description->set('std.identifier', new StringDescriptor($node->getIdentifier()));
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function supports(Subject $subject)
68
    {
69
        return $this->documentManager->getMetadataFactory()->hasMetadataFor(ClassUtils::getRealClass($subject->getClass()->getName()));
0 ignored issues
show
Bug introduced by
Consider using $subject->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
70
    }
71
}
72