Completed
Push — master ( b1e1a4...dbe8ee )
by Eric
10:13 queued 49s
created

ResourceSubscriber::loadClassMetadata()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 22
loc 22
ccs 14
cts 14
cp 1
rs 8.6737
cc 5
eloc 11
nc 5
nop 1
crap 5
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