Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

AdminBundle/EventListener/MappingListener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\EventListener;
4
5
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
6
7
/**
8
 * Class MappingListener
9
 */
10
class MappingListener
11
{
12
    /**
13
     * @var string
14
     */
15
    private $className;
16
17
    /**
18
     * Constructor
19
     *
20
     * @param string $className
21
     */
22 13
    public function __construct($className)
23
    {
24 13
        $this->className = $className;
25 13
    }
26
27
    /**
28
     * Called when class meta data is fetched.
29
     *
30
     * @param LoadClassMetadataEventArgs $eventArgs
31
     */
32 13
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
33
    {
34 13
        $classMetadata = $eventArgs->getClassMetadata();
35 13
        $entityName = (string) $classMetadata->getName();
36
37
        // We dynamically set the user class that was configured in the configuration
38 13 View Code Duplication
        if ($entityName == 'Kunstmaan\AdminBundle\Entity\AclChangeset') {
0 ignored issues
show
This code seems to be duplicated across 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...
39
            $mapping = array(
40 1
                'fieldName' => 'user',
41 1
                'targetEntity' => $this->className,
42
                'joinColumns' => array(array(
43
                    'name' => 'user_id',
44
                    'referencedColumnName' => 'id',
45
                    'unique' => false,
46
                    'nullable' => true,
47
                )),
48
            );
49 1
            $classMetadata->mapManyToOne($mapping);
50
        }
51
52
        // If we overwrite the user entity, we should create a new join table with the groups
53 13
        if ($entityName == $this->className) {
54 1
            $tableName = $classMetadata->table['name'];
55
            $mapping = array(
56 1
                'fieldName' => 'groups',
57
                'joinTable' => array(
58 1
                    'name' => $tableName . '_groups',
59
                    'joinColumns' => array(array(
60
                        'name' => 'user_id',
61
                        'unique' => false,
62
                        'nullable' => true,
63
                        'referencedColumnName' => 'id',
64
                    )),
65
                    'inverseJoinColumns' => array(array(
66
                        'name' => 'group_id',
67
                        'unique' => false,
68
                        'nullable' => true,
69
                        'referencedColumnName' => 'id',
70
                    )),
71
                ),
72 1
                'targetEntity' => 'Kunstmaan\AdminBundle\Entity\Group',
73 1
                'sourceEntity' => $this->className,
74
            );
75 1
            $classMetadata->mapManyToMany($mapping);
76
        }
77 13
    }
78
}
79