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 | 13 | public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
|
31 | { |
||
32 | 13 | $classMetadata = $eventArgs->getClassMetadata(); |
|
33 | 13 | $entityName = (string) $classMetadata->getName(); |
|
34 | |||
35 | // We dynamically set the user class that was configured in the configuration |
||
36 | 13 | View Code Duplication | if ($entityName == 'Kunstmaan\AdminBundle\Entity\AclChangeset') { |
0 ignored issues
–
show
|
|||
37 | $mapping = [ |
||
38 | 1 | 'fieldName' => 'user', |
|
39 | 1 | 'targetEntity' => $this->className, |
|
40 | 'joinColumns' => [[ |
||
41 | 'name' => 'user_id', |
||
42 | 'referencedColumnName' => 'id', |
||
43 | 'unique' => false, |
||
44 | 'nullable' => true, |
||
45 | ]], |
||
46 | ]; |
||
47 | 1 | $classMetadata->mapManyToOne($mapping); |
|
48 | } |
||
49 | |||
50 | // If we overwrite the user entity, we should create a new join table with the groups |
||
51 | 13 | if ($entityName == $this->className) { |
|
52 | 1 | $tableName = $classMetadata->table['name']; |
|
53 | $mapping = [ |
||
54 | 1 | 'fieldName' => 'groups', |
|
55 | 'joinTable' => [ |
||
56 | 1 | 'name' => $tableName . '_groups', |
|
57 | 'joinColumns' => [[ |
||
58 | 'name' => 'user_id', |
||
59 | 'unique' => false, |
||
60 | 'nullable' => true, |
||
61 | 'referencedColumnName' => 'id', |
||
62 | ]], |
||
63 | 'inverseJoinColumns' => [[ |
||
64 | 'name' => 'group_id', |
||
65 | 'unique' => false, |
||
66 | 'nullable' => true, |
||
67 | 'referencedColumnName' => 'id', |
||
68 | ]], |
||
69 | ], |
||
70 | 1 | 'targetEntity' => 'Kunstmaan\AdminBundle\Entity\Group', |
|
71 | 1 | 'sourceEntity' => $this->className, |
|
72 | ]; |
||
73 | 1 | $classMetadata->mapManyToMany($mapping); |
|
74 | } |
||
75 | 13 | } |
|
76 | } |
||
77 |
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.