Completed
Push — master ( 167738...fe3be6 )
by Paweł
58:17 queued 58:01
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\EventListener;
13
14
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
15
use Doctrine\ORM\Events;
16
use Doctrine\ORM\Mapping\ClassMetadata;
17
18
/**
19
 * @author Ben Davies <[email protected]>
20
 */
21
class ORMRepositoryClassSubscriber extends AbstractDoctrineSubscriber
22
{
23
    /**
24
     * @return array
25
     */
26
    public function getSubscribedEvents()
27
    {
28
        return [
29
            Events::loadClassMetadata,
30
        ];
31
    }
32
33
    /**
34
     * @param LoadClassMetadataEventArgs $eventArgs
35
     */
36
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
37
    {
38
        $this->setCustomRepositoryClass($eventArgs->getClassMetadata());
0 ignored issues
show
Compatibility introduced by
$eventArgs->getClassMetadata() of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ORM\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...
39
    }
40
41
    /**
42
     * @param ClassMetadata $metadata
43
     */
44
    private function setCustomRepositoryClass(ClassMetadata $metadata)
45
    {
46
        try {
47
            $resourceMetadata = $this->resourceRegistry->getByClass($metadata->getName());
48
        } catch (\InvalidArgumentException $exception) {
49
            return;
50
        }
51
52
        if ($resourceMetadata->hasClass('repository')) {
53
            $metadata->setCustomRepositoryClass($resourceMetadata->getClass('repository'));
0 ignored issues
show
Bug introduced by
It seems like $resourceMetadata->getClass('repository') targeting Sylius\Component\Resourc...taInterface::getClass() can also be of type array; however, Doctrine\ORM\Mapping\Cla...CustomRepositoryClass() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54
        }
55
    }
56
}
57