ClassMetadataFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 17.39 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetadataFor() 8 21 4
A setDocumentManager() 0 5 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
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Mapping;
12
13
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory as BaseClassMetadataFactory;
14
use Doctrine\ODM\MongoDB\DocumentManager;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Events;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\PreLoadClassMetadataEventArgs;
17
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
18
19
/**
20
 * ClassMetadata Factory Class.
21
 *
22
 * @author Karel Osorio Ramírez <[email protected]>
23
 */
24
class ClassMetadataFactory extends BaseClassMetadataFactory
25
{
26
    /**
27
     * @var ClassMetadata[]
28
     */
29
    private $innerLoadedMetadata = [];
30
31
    /**
32
     * @var DocumentManager
33
     */
34
    private $documentManager;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getMetadataFor($className)
40
    {
41
        if (!isset($this->innerLoadedMetadata[$className])) {
42
            $evm = $this->documentManager->getEventManager();
43 View Code Duplication
            if ($evm->hasListeners(Events::PRE_LOAD_CLASSMETADATA)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44
                $eventArgs = new PreLoadClassMetadataEventArgs($className, $this->documentManager);
45
                $evm->dispatchEvent(Events::PRE_LOAD_CLASSMETADATA, $eventArgs);
46
            }
47
48
            $classMetadata = parent::getMetadataFor($className);
49
50 View Code Duplication
            if ($evm->hasListeners(Events::POST_LOAD_CLASS_METADATA)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
                $eventArgs = new LoadClassMetadataEventArgs($classMetadata, $this->documentManager);
52
                $evm->dispatchEvent(Events::POST_LOAD_CLASS_METADATA, $eventArgs);
53
            }
54
55
            $this->innerLoadedMetadata[$className] = $classMetadata;
56
        }
57
58
        return $this->innerLoadedMetadata[$className];
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->innerLoadedMetadata[$className]; of type Cubiche\Infrastructure\D...e\Mapping\ClassMetadata adds the type Cubiche\Infrastructure\D...B\Mapping\ClassMetadata to the return on line 58 which is incompatible with the return type declared by the interface Doctrine\Common\Persiste...Factory::getMetadataFor of type Doctrine\Common\Persistence\Mapping\ClassMetadata.
Loading history...
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function setDocumentManager(DocumentManager $dm)
65
    {
66
        parent::setDocumentManager($dm);
67
        $this->documentManager = $dm;
68
    }
69
}
70