1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusVendorPlugin\EventListener; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\EventSubscriber; |
8
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
9
|
|
|
use Doctrine\ORM\Events; |
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
11
|
|
|
use Odiseo\SyliusVendorPlugin\Model\VendorInterface; |
12
|
|
|
use Odiseo\SyliusVendorPlugin\Model\VendorsAwareInterface; |
13
|
|
|
use Sylius\Component\Channel\Model\ChannelInterface; |
14
|
|
|
use Sylius\Component\Product\Model\ProductInterface; |
15
|
|
|
use Sylius\Component\Resource\Metadata\RegistryInterface; |
16
|
|
|
|
17
|
|
|
final class ORMVendorAwareListener implements EventSubscriber |
18
|
|
|
{ |
19
|
|
|
/** @var RegistryInterface */ |
20
|
|
|
private $resourceMetadataRegistry; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $vendorClass; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $productClass; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $channelClass; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param RegistryInterface $resourceMetadataRegistry |
33
|
|
|
* @param $vendorClass |
34
|
|
|
* @param $productClass |
35
|
|
|
* @param $channelClass |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
RegistryInterface $resourceMetadataRegistry, |
39
|
|
|
$vendorClass, |
40
|
|
|
$productClass, |
41
|
|
|
$channelClass |
42
|
|
|
) { |
43
|
|
|
$this->resourceMetadataRegistry = $resourceMetadataRegistry; |
44
|
|
|
$this->vendorClass = $vendorClass; |
45
|
|
|
$this->productClass = $productClass; |
46
|
|
|
$this->channelClass = $channelClass; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getSubscribedEvents(): array |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
Events::loadClassMetadata, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add mapping to translatable entities |
61
|
|
|
* |
62
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
63
|
|
|
*/ |
64
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
65
|
|
|
{ |
66
|
|
|
$classMetadata = $eventArgs->getClassMetadata(); |
67
|
|
|
$reflection = $classMetadata->reflClass; |
68
|
|
|
|
69
|
|
|
if (!$reflection || $reflection->isAbstract()) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ( |
74
|
|
|
$reflection->implementsInterface(ProductInterface::class) && |
75
|
|
|
$reflection->implementsInterface(VendorsAwareInterface::class) |
76
|
|
|
) { |
77
|
|
|
$this->mapVendorAware($classMetadata, 'products'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ( |
81
|
|
|
$reflection->implementsInterface(ChannelInterface::class) && |
82
|
|
|
$reflection->implementsInterface(VendorsAwareInterface::class) |
83
|
|
|
) { |
84
|
|
|
$this->mapVendorAware($classMetadata, 'channels'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($reflection->implementsInterface(VendorInterface::class)) { |
88
|
|
|
$this->mapVendor($classMetadata); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add mapping data to a translatable entity. |
94
|
|
|
* |
95
|
|
|
* @param ClassMetadata $metadata |
96
|
|
|
* @param string $mappedBy |
97
|
|
|
*/ |
98
|
|
|
private function mapVendorAware(ClassMetadata $metadata, string $mappedBy): void |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$vendorMetadata = $this->resourceMetadataRegistry->getByClass($this->vendorClass); |
102
|
|
|
} catch (\InvalidArgumentException $exception) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!$metadata->hasAssociation('vendors')) { |
107
|
|
|
$metadata->mapManyToMany([ |
108
|
|
|
'fieldName' => 'vendors', |
109
|
|
|
'targetEntity' => $vendorMetadata->getClass('model'), |
110
|
|
|
'mappedBy' => $mappedBy, |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add mapping data to a translatable entity. |
117
|
|
|
* |
118
|
|
|
* @param ClassMetadata $metadata |
119
|
|
|
*/ |
120
|
|
|
private function mapVendor(ClassMetadata $metadata): void |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
$productMetadata = $this->resourceMetadataRegistry->getByClass($this->productClass); |
124
|
|
|
$channelMetadata = $this->resourceMetadataRegistry->getByClass($this->channelClass); |
125
|
|
|
} catch (\InvalidArgumentException $exception) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!$metadata->hasAssociation('products')) { |
130
|
|
|
$metadata->mapManyToMany([ |
131
|
|
|
'fieldName' => 'products', |
132
|
|
|
'targetEntity' => $productMetadata->getClass('model'), |
133
|
|
|
'inversedBy' => 'vendors', |
134
|
|
|
'joinTable' => [ |
135
|
|
|
'name' => 'odiseo_vendors_products', |
136
|
|
|
'joinColumns' => [[ |
137
|
|
|
'name' => 'vendor_id', |
138
|
|
|
'referencedColumnName' => 'id', |
139
|
|
|
'onDelete' => 'CASCADE', |
140
|
|
|
'nullable' => false, |
141
|
|
|
]], |
142
|
|
|
'inverseJoinColumns' => [[ |
143
|
|
|
'name' => 'product_id', |
144
|
|
|
'referencedColumnName' => 'id', |
145
|
|
|
]], |
146
|
|
|
] |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (!$metadata->hasAssociation('channels')) { |
151
|
|
|
$metadata->mapManyToMany([ |
152
|
|
|
'fieldName' => 'channels', |
153
|
|
|
'targetEntity' => $channelMetadata->getClass('model'), |
154
|
|
|
'inversedBy' => 'vendors', |
155
|
|
|
'joinTable' => [ |
156
|
|
|
'name' => 'odiseo_vendors_channels', |
157
|
|
|
'joinColumns' => [[ |
158
|
|
|
'name' => 'vendor_id', |
159
|
|
|
'referencedColumnName' => 'id', |
160
|
|
|
'onDelete' => 'CASCADE', |
161
|
|
|
'nullable' => false, |
162
|
|
|
]], |
163
|
|
|
'inverseJoinColumns' => [[ |
164
|
|
|
'name' => 'channel_id', |
165
|
|
|
'referencedColumnName' => 'id', |
166
|
|
|
]], |
167
|
|
|
] |
168
|
|
|
]); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|