MetadataSubscriber::loadClassMetadata()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 14
nc 5
nop 1
1
<?php
2
3
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Subscriber;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
7
use Doctrine\ODM\PHPCR\Event;
8
use Metadata\MetadataFactory;
9
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\FieldMapper;
10
use Psi\Component\ContentType\FieldLoader;
11
12 View Code Duplication
class MetadataSubscriber implements EventSubscriber
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    private $metadataFactory;
15
    private $fieldLoader;
16
    private $mapper;
17
18
    public function __construct(
19
        MetadataFactory $metadataFactory,
20
        FieldLoader $fieldLoader,
21
        FieldMapper $mapper
22
    ) {
23
        $this->metadataFactory = $metadataFactory;
24
        $this->fieldLoader = $fieldLoader;
25
        $this->mapper = $mapper;
26
    }
27
28
    public function getSubscribedEvents()
29
    {
30
        return [
31
            Event::loadClassMetadata,
32
        ];
33
    }
34
35
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
36
    {
37
        $metadata = $args->getClassMetadata();
38
39
        if (null === $metadata = $this->metadataFactory->getMetadataForClass($metadata->getName())) {
40
            return;
41
        }
42
43
        $odmMetadata = $args->getClassMetadata();
44
45
        foreach ($metadata->getPropertyMetadata() as $property) {
46
            try {
47
                $field = $this->fieldLoader->load($property->getType(), $property->getOptions());
48
                $this->mapper->__invoke($property->getName(), $field, $odmMetadata);
0 ignored issues
show
Compatibility introduced by
$odmMetadata of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\PHPCR\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
49
            } catch (\Exception $e) {
50
                throw new \InvalidArgumentException(sprintf(
51
                    'Could not map field type "%s" on property "%s#%s"',
52
                    $property->getType(), $property->getClass(), $property->getName()
53
                ), null, $e);
54
            }
55
        }
56
    }
57
}
58