MetadataEventListener   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B loadClassMetadata() 0 24 6
A getMetadataFactory() 0 15 3
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Infrastructure\Doctrine\ODM\MongoDB\EventListener;
12
13
use Cubiche\Core\Metadata\ClassMetadataFactory;
14
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\RegisterDriverMetadataEventArgs;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Events;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Factory\DriverFactory;
17
use Doctrine\Common\EventManager;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
20
21
/**
22
 * MetadataEventListener class.
23
 *
24
 * @author Ivannis Suárez Jerez <[email protected]>
25
 */
26
class MetadataEventListener
27
{
28
    /**
29
     * @var ClassMetadataFactory
30
     */
31
    protected $metadataFactory;
32
33
    /**
34
     * Add mapping to translatable entities.
35
     *
36
     * @param LoadClassMetadataEventArgs $eventArgs
37
     */
38
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
39
    {
40
        $classMetadata = $eventArgs->getClassMetadata();
41
        $reflClass = $classMetadata->reflClass;
0 ignored issues
show
Bug introduced by
Accessing reflClass 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...
42
        if (!$reflClass || $reflClass->isAbstract()) {
43
            return;
44
        }
45
46
        $cubicheClassMetadata = $this
47
            ->getMetadataFactory(
48
                $eventArgs->getDocumentManager(),
49
                $eventArgs->getDocumentManager()->getEventManager()
50
            )->getMetadataFor($classMetadata->getName())
51
        ;
52
53
        foreach ($classMetadata->fieldMappings as $fieldName => &$mapping) {
0 ignored issues
show
Bug introduced by
Accessing fieldMappings 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...
54
            if ($cubicheClassMetadata !== null) {
55
                $propertyMetadata = $cubicheClassMetadata->propertyMetadata($fieldName);
56
                if ($propertyMetadata !== null) {
57
                    $mapping['cubiche:'.$propertyMetadata->getMetadata('namespace')] = $propertyMetadata;
58
                }
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param ObjectManager $om
65
     *
66
     * @return ClassMetadataFactory
67
     */
68
    protected function getMetadataFactory(ObjectManager $om, EventManager $evm)
69
    {
70
        if ($this->metadataFactory === null) {
71
            $driverFactory = DriverFactory::instance();
72
73
            if ($evm->hasListeners(Events::REGISTER_DRIVER_METADATA)) {
74
                $eventArgs = new RegisterDriverMetadataEventArgs($driverFactory, $om);
75
                $evm->dispatchEvent(Events::REGISTER_DRIVER_METADATA, $eventArgs);
76
            }
77
78
            $this->metadataFactory = new ClassMetadataFactory($driverFactory->driversFromManager($om));
79
        }
80
81
        return $this->metadataFactory;
82
    }
83
}
84