Completed
Pull Request — master (#44)
by Eric
10:31
created

ResourceSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
c 3
b 1
f 0
lcom 1
cbo 4
dl 63
loc 63
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
B loadClassMetadata() 22 22 5
A getSubscribedEvents() 4 4 1
A resolveResource() 8 8 3

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 Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\EventSubscriber\Doctrine\MongoDB;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
16
use Doctrine\ODM\MongoDB\Events;
17
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
18
use Doctrine\ODM\MongoDB\Mapping\MappingException;
19
use Lug\Component\Registry\Model\RegistryInterface;
20
use Lug\Component\Resource\Model\ResourceInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25 View Code Duplication
class ResourceSubscriber 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...
26
{
27
    /**
28
     * @var RegistryInterface
29
     */
30
    private $resourceRegistry;
31
32
    /**
33
     * @param RegistryInterface $registry
34
     */
35 8
    public function __construct(RegistryInterface $registry)
36
    {
37 8
        $this->resourceRegistry = $registry;
38 8
    }
39
40
    /**
41
     * @param LoadClassMetadataEventArgs $event
42
     */
43 5
    public function loadClassMetadata(LoadClassMetadataEventArgs $event)
44
    {
45
        /** @var ClassMetadata $classMetadata */
46 5
        $classMetadata = $event->getClassMetadata();
47
48 5
        if (($resource = $this->resolveResource($classMetadata->getName())) !== null) {
49 4
            foreach ($classMetadata->parentClasses as $parentClass) {
50
                try {
51 3
                    $parentMetadata = $event->getObjectManager()->getClassMetadata($parentClass);
52 3
                } catch (MappingException $e) {
53 1
                    continue;
54
                }
55
56
                /** @var ClassMetadata $parentMetadata */
57 2
                if ($parentMetadata->inheritanceType === ClassMetadata::INHERITANCE_TYPE_NONE) {
58 1
                    $parentMetadata->isMappedSuperclass = true;
59 1
                }
60 4
            }
61
62 4
            $classMetadata->setCustomRepositoryClass($resource->getRepository());
63 4
        }
64 5
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function getSubscribedEvents()
70
    {
71 1
        return [Events::loadClassMetadata];
72
    }
73
74
    /**
75
     * @param string $class
76
     *
77
     * @return ResourceInterface|null
78
     */
79 5
    private function resolveResource($class)
80
    {
81 5
        foreach ($this->resourceRegistry as $resource) {
82 4
            if ($resource->getModel() === $class) {
83 4
                return $resource;
84
            }
85 1
        }
86 1
    }
87
}
88