RoutableSubscriber::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CoreBundle\Doctrine\EventSubscriber;
14
15
use Doctrine\Common\EventSubscriber;
16
use Doctrine\ORM\Event\LifecycleEventArgs;
17
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
18
use Doctrine\ORM\Events;
19
use WellCommerce\Bundle\CoreBundle\Entity\RoutableSubjectInterface;
20
use WellCommerce\Bundle\CoreBundle\Entity\RoutingDiscriminatorsAwareInterface;
21
22
/**
23
 * Class RoutableSubscriber
24
 *
25
 * @author  Adam Piotrowski <[email protected]>
26
 */
27
final class RoutableSubscriber implements EventSubscriber
28
{
29
    /**
30
     * @var array
31
     */
32
    private $routingDiscriminatorMap;
33
    
34
    public function __construct(array $routingDiscriminatorMap = [])
35
    {
36
        $this->routingDiscriminatorMap = $routingDiscriminatorMap;
37
    }
38
    
39
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
40
    {
41
        $metadata = $eventArgs->getClassMetaData();
42
        /** @var $reflectionClass \ReflectionClass */
43
        $reflectionClass = $metadata->getReflectionClass();
0 ignored issues
show
Bug introduced by
The method getReflectionClass() does not seem to exist on object<Doctrine\ORM\EntityManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        if ($reflectionClass->implementsInterface(RoutingDiscriminatorsAwareInterface::class)) {
45
            $metadata->setDiscriminatorMap($this->routingDiscriminatorMap);
0 ignored issues
show
Bug introduced by
The method setDiscriminatorMap() does not seem to exist on object<Doctrine\ORM\EntityManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        }
47
    }
48
    
49
    public function prePersist(LifecycleEventArgs $args)
50
    {
51
        $entity = $args->getEntity();
52
        if ($entity instanceof RoutableSubjectInterface) {
53
            $route = $this->addRoute($entity);
54
            $entity->setRoute($route);
55
        }
56
    }
57
    
58
    protected function addRoute(RoutableSubjectInterface $entity)
59
    {
60
        $route = $entity->getRouteEntity();
61
        $route->setPath($entity->getSlug());
62
        $route->setLocale($entity->getLocale());
63
        $route->setIdentifier($entity->getTranslatable());
64
        
65
        return $route;
66
    }
67
    
68
    public function getSubscribedEvents()
69
    {
70
        return [
71
            Events::prePersist,
72
            Events::loadClassMetadata,
73
        ];
74
    }
75
}
76