ShopableSubscriber::loadClassMetadata()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 5
nop 1
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\AppBundle\EventListener;
14
15
use Doctrine\Common\EventSubscriber;
16
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
17
use Doctrine\ORM\Events;
18
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19
use WellCommerce\Bundle\AppBundle\Entity\Shop;
20
21
/**
22
 * Class ShopableSubscriber
23
 *
24
 * @author  Adam Piotrowski <[email protected]>
25
 */
26
class ShopableSubscriber implements EventSubscriber
27
{
28
    /**
29
     * @var \Doctrine\ORM\Mapping\ClassMetadataInfo
30
     */
31
    protected $classMetadata;
32
    
33
    public function getSubscribedEvents()
34
    {
35
        return [Events::loadClassMetadata];
36
    }
37
    
38
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
39
    {
40
        $this->classMetadata = $eventArgs->getClassMetadata();
0 ignored issues
show
Documentation Bug introduced by
It seems like $eventArgs->getClassMetadata() of type object<Doctrine\ORM\EntityManager> is incompatible with the declared type object<Doctrine\ORM\Mapping\ClassMetadataInfo> of property $classMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $reflectionClass     = $this->classMetadata->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...
42
        
43
        if (null === $reflectionClass) {
44
            return;
45
        }
46
        
47
        if ($reflectionClass->hasMethod('setShops') && $reflectionClass->hasMethod('getShops')) {
48
            $this->mapShopAssociation();
49
        }
50
        
51
        if ($reflectionClass->hasMethod('setShop') && $reflectionClass->hasMethod('getShop')) {
52
            $this->mapShopField();
53
        }
54
    }
55
    
56
    protected function mapShopAssociation()
57
    {
58
        if (!$this->classMetadata->hasAssociation('shops')) {
59
            $this->classMetadata->mapManyToMany([
60
                'fieldName'          => 'shops',
61
                'fetch'              => ClassMetadataInfo::FETCH_LAZY,
62
                'joinColumns'        => [
63
                    [
64
                        'name'                 => 'foreign_id',
65
                        'referencedColumnName' => 'id',
66
                        'onDelete'             => 'CASCADE',
67
                        'nullable'             => false,
68
                    ],
69
                ],
70
                'inverseJoinColumns' => [
71
                    [
72
                        'name'                 => 'shop_id',
73
                        'referencedColumnName' => 'id',
74
                        'onDelete'             => 'CASCADE',
75
                        'nullable'             => false,
76
                    ],
77
                ],
78
                'targetEntity'       => Shop::class,
79
            ]);
80
        }
81
    }
82
    
83
    protected function mapShopField()
84
    {
85
        if (!$this->classMetadata->hasAssociation('shop')) {
86
            $this->classMetadata->mapManyToOne([
87
                'fieldName'    => 'shop',
88
                'fetch'        => ClassMetadataInfo::FETCH_LAZY,
89
                'joinColumns'  => [
90
                    [
91
                        'name'                 => 'shop_id',
92
                        'referencedColumnName' => 'id',
93
                        'onDelete'             => 'CASCADE',
94
                    ],
95
                ],
96
                'targetEntity' => Shop::class,
97
            ]);
98
        }
99
    }
100
}
101